Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions fetch/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>XKCD Latest Comic</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h1>Latest XKCD Comic</h1>
<div id="image-container"></div>
<script src="script.js"></script>
</body>
</html>
35 changes: 35 additions & 0 deletions fetch/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
async function fetchLatestXKCD() {
const apiEndpoint = 'https://xkcd.now.sh/?comic=latest';

try {
const response = await fetch(apiEndpoint);
if (!response.ok) {
throw new Error(`Fetching error! Status: ${response.status}`);
}

const data = await response.json();
console.log('Received data:', data);

if (data.img) {
const imgElement = document.createElement('img');
imgElement.src = data.img;
imgElement.alt = data.alt || 'XKCD Comic';

const container = document.getElementById('image-container');
container.innerHTML = '';
container.appendChild(imgElement);
} else {
console.warn('No "img" property found in the data.');
}

} catch (error) {
console.error('Error fetching data:', error);
const errorMsg = document.createElement('div');
errorMsg.className = 'error';
errorMsg.textContent = `Error fetching data: ${error.message}`;
document.body.appendChild(errorMsg);
}
}

fetchLatestXKCD();

41 changes: 41 additions & 0 deletions fetch/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
body {
font-family: Arial, sans-serif;
background-color: #f0f2f5;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
padding: 20px;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor issue: Be careful when using height and padding - on some devices this causes scrollbars which are unnecessary for the size of this webpage.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've modified min-height to use dynamic viewport height(dvh) to avoid unnecessary scroll and use box-sizing: border-box to prevent padding increasing the overall height.

}

h1 {
color: #333;
}

#image-container {
margin-top: 20px;
display: flex;
flex-wrap: wrap;
gap: 20px;
justify-content: center;
}

#image-container img {
max-width: 300px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
transition: transform 0.2s, box-shadow 0.2s;
}

#image-container img:hover {
transform: scale(1.05);
box-shadow: 0 8px 20px rgba(0,0,0,0.3);
}

.error {
color: red;
font-weight: bold;
margin-top: 20px;
}