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.

CSS

Luke Dawes
Luke Dawes
9,738 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,738 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
216,121 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;
}