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

HTML How to Make a Website Styling Web Pages and Navigation Style the Image Captions

How would you center the last image?

There are 5 images on the page; 2 rows of 2 images & 1 row with 1 image (on the left). What would you write in order to center the image on the 3rd row? Would you have to id the 3rd list item in the HTML, and then make a specific rule for it in the CSS? Is there a different way to do it?

1 Answer

Damien Watson
Damien Watson
27,419 Points

Hi, the items are being laid out using 'float:left'. Items floating left will sit beside each other if they have space and if not drop down to the next line and become left aligned. This is for dynamic layout. You can center the last image but what happens if there are 6 images and the last is now not alone.

If you really want the last item to be centered then you need to modify the styles a little. Instead of using 'float' use 'display:inline-block' on your main 'li' elements. You also need to reduce the margin a little. Then add the 'last-child' style in and it should be centered.

Note: Haven't cross browser checked this but it might get you on the right track.

#gallery li {
  display: inline-block;
  width: 45%;
  margin: 2%;
  background-color: #f5f5f5;
  color: #bdc3c7;
}

#gallery li:last-child {
  display: block;
  margin: 0 auto;
}

I changed the margin of the last-child to 2% auto, and it worked perfectly. Thank you.