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 Responsive Web Design and Testing Build a Three Column Layout

Ellen Topitzer
Ellen Topitzer
1,205 Points

Gallery two column layout not aligning properly

So I've done everything I can think of to fix this problem, but no matter what I do the 5th and 6th images in my gallery two column layout are not aligning properly. I've included a link to my code below. I've already updated my nth-child in my responsive css page to #gallery li:nth-child(3n+1) . I've double checked my code and rewatched the video and I just can't figure this out! Any help would be incredibly appreciated.

https://w.trhou.se/c5h7jqla4e

1 Answer

Hi Ellen,

I think the problem you're having is related to the problem that Nick shows in the video around 8:40.

When the 2nd item caption wraps to 3 lines it extends down lower and effectively prevents the 4th item from floating all the way to the left.

In your case, it's not the captions but the height of the images differ. Your images are not the same native size.

When "The Gift" and "The Song" are scaled to the same width, The Gift image ends up being about 0.1px taller than The Song. It's too small a difference to see by looking at it but if you use your browser's developer tools you'll be able to see that the computed heights of the images differ by about 1/10 of a pixel.

This has the same effect as if the caption wrapped to another line. It blocks your 5th image, Little Red, from floating all the way to the left.

You can employ a similar nth-child solution for the 2-column layout as was done for the 3 column layout to fix the problem.

In main.css add: (expression is changed to 2n + 1 for 2 columns)

#gallery li:nth-child(2n+1) {
    clear: left;
}

This is still going to be applied when you reach the media query and it's going to mess up the 3 column layout. So you have to first select the 2n + 1 items and set the clear property back to none so that those items are no longer clearing the floats then you can set the 3n + 1 items to clear the floats.

In responsive.css

#gallery li:nth-child(2n+1) {
    clear: none;
}

#gallery li:nth-child(3n+1) {
    clear: left;
}

Let me know if you're still having problems.

Ellen Topitzer
Ellen Topitzer
1,205 Points

Hi Jason, thank you so much for your help, I really appreciate it! I went in and edited my images so they were exactly the same size and now the problem has been resolved. Again, thank you so much!