Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Luke Dawes
Luke Dawes
9,739 Points

Centering the image on the page.

Hi all,

I successfully completed the "Creating a Simple Lightbox" segment of the jQuery Basics course, and I'm now trying to replicate my work using Brackets, a code editor, on my laptop. Almost everything works, e.g. the overlay appears when I click, along with the correct image and the caption in a light-box, but the overlay does not bring the image to the centre of the page, no matter what I try.

#overlay {
 background: rgba(0,0,0,0.7);
 width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  display: none;
}

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

#overlay p {
 color: white; 
}

I have cut-and-paste the code from Workspaces to the Brackets environment. I've organised my folders and images correctly -- which I know for sure because they all appear correctly and the page has (almost) the right CSS layout and the jQuery lightbox appears on a click -- and I have repeatedly saved, rewritten and re-saved, then closed and re-opened the Brackets editor and the browsers I've tested it in (Chrome and Safari). So far, no luck. I even experimented with later versions of jQuery by editing the link in my HTML file.

Does anybody have any suggestions? I'm not even sure what the problem is at this point.

Michael Fish
Michael Fish
7,804 Points

Hey Luke,

If it's not too much trouble could you paste your code into a codepen over at http://codepen.io/

I would be happy to take a look at it

3 Answers

Luke Dawes
Luke Dawes
9,739 Points

Hi Michael,

I've created an account for myself and created the following pen -- https://codepen.io/Llanyewe7/pen/YqRJQm -- but I'm not sure how (or even if) I can include the images I'm working with in the lightbox. Let me know if I need to do that in order for you to help.

Thanks!

Steven Parker
Steven Parker
229,732 Points

Centering with absolute positioning is a bit different from centering flowed items. Try this:

#overlay img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%)
}
Rudy Tan
PLUS
Rudy Tan
Courses Plus Student 16,635 Points

You can try by using display for the img element.

#overlay img {
    display: block;
    margin: 10% auto;
}