Tell us what’s happening:
I am having some issues passing two test cases for this lab which pertain to the .lightbox element having a background color and the element’s display being set back to none when it is visible.
I would like to preface that this is not my code, as I was looking at solutions to figure out how this Lab could be done. This approach was done by leontoys.
NOTE:
I realized when I used the ‘Ask for help’ feature it assigned my post to HTML-CSS, so I deleted the post in the different category and re-posted this here since the Lab is within the JavaScript section.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lightbox Viewer</title>
<link href="styles.css" rel="stylesheet">
</head>
<body>
<h1>Image Gallery</h1>
<div class="gallery">
<img src="https://cdn.freecodecamp.org/curriculum/labs/stonehenge-thumbnail.jpg" class="gallery-item">
<img src="https://cdn.freecodecamp.org/curriculum/labs/storm-thumbnail.jpg" class="gallery-item">
<img src="https://cdn.freecodecamp.org/curriculum/labs/trees-thumbnail.jpg" class="gallery-item">
</div>
<div class="lightbox">
<button id="close-btn">×</button>
<img id="lightbox-image">
</div>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
min-height: 100vh;
}
.lightbox {
width: 100%;
height: 100%;
display: none;
top: 0;
left: 0;
position: fixed;
background-color: sky-blue;
}
.gallery {
border: 1px solid skyblue;
padding: 5%;
margin: 5%;
display: flex;
justify-content: space-between;
}
.gallery-item {
height: 200px;
width: auto;
margin: 5%;
border: 1px solid skyblue;
padding: 1%;
}
h1 {
margin: 5%;
text-align: center;
background-color: skyblue;
}
/* file: script.js */
const imgList = document.querySelectorAll(".gallery-item");
const lightBox = document.querySelector(".lightbox");
const lightBoxImage = document.querySelector("#lightbox-image");
const buttonEl = document.querySelector("#close-btn");
const itemClick = (event) => {
const imageThumbnail = event.target.src;
lightBox.style.display = "flex";
const image = imageThumbnail.replace("-thumbnail", "");
lightBoxImage.src = image;
event.stopPropagation();
};
imgList.forEach(image => {
image.addEventListener("click", itemClick);
});
buttonEl.addEventListener("click", (e) => {
if (lightBox.style.display === "flex" && e.target !== lightBoxImage)
lightBox.style.display = "none";
});
Console:
12. Your .lightbox element should have a background color.
17. When your .lightbox element is visible and you click the .lightbox element, the .lightbox element's display should be set back to none.
Challenge Information:
Build a Lightbox Viewer - Build a Lightbox Viewer

