Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript jQuery Basics (2014) Creating a Simple Lightbox Perform: Part 4

Lightbox/Overlay not working properly

Hi, I followed the lessons down to a 'T' earned the badges too, but i just can't seem to 'get rid' of the overlay when the page loads up. I get the opaque overlay background and i'm unable to click on any image. my jQuery and CSS code is below:

//Problem: User when clicking on image goes to a dead end
//Solution: Create an overlay with the large image - Lightbox

var $overlay = $('<div id="overlay"></div>');
var $image = $("<img>");
var $caption = $("<p></p>")
//An image to overlay
$overlay.append($image);

//Add overlay
$("body").append($overlay);
  //A caption
$overlay.append($caption);

//Capture the click event on a link to an image
$("#imageGallery a").click(function(event){
  event.preventDefault();
  var imageLocation = $(this).attr("href");
  //Update overlay with the image linked in the link
  $image.attr("src", imageLocation);

  //Show the overlay.
  $overlay.show();
  var captionText = $(this).children("img").attr("alt");
  $caption.text(captionText);

  //Get child's alt attribute and set caption


});

//When overlay is clicked
$overlay.click(function(){
  //Hide the overlay
  $overlay.hide();
});

My CSS:
#overlay {
  background:rgba(0,0,0,0.7);
  width: 100%;
  height:100%;
  position:absolute;
  top:0;
  left: 0;
  text-align:center;

}

#overlay img {
  margin-top: 10%;
}

#overlay p {
  color: white;  
}

Thanks

James Barnett
James Barnett
39,199 Points

Bola Ajayi - For code that's more than a few lines it's often best to throw it into codepen it's easier to debug a "working" demo.

3 Answers

Andrew Shook
Andrew Shook
31,709 Points

Add display: none and z-index: 5 to #overlay in your css.

Thanks Andrew - that worked a treat! James - Didn't know, next time - I will do! :)

Devonte M
Devonte M
4,197 Points

I had the same problem! Thanks.