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 How to Make a Website Styling Web Pages and Navigation Style the Portfolio

Cheryl Hurley
Cheryl Hurley
2,250 Points

<li> sorting irregularly

I'm following along, as always, but using my own photos. I just sorted the unordered list into two columns, each taking up 45% with a 2.5% margin. However.. my <li> are sorted with three on the right-hand column and two on the left-hand column, skipping the row in the middle on the left side. Very odd. How did this happen?

Jacob Miller
Jacob Miller
12,466 Points

Can you post your code please?

6 Answers

Hi Cheryl,

The general problem here is that we're floating items of variable height. Usually this is due to the captions being different lengths and wrapping to a different number of lines. This makes some items taller than others. In your case, it may also be due to the images themselves not having the exact same heights. Might be off by a pixel or 2. Your low and high range do not have the exact same aspect ratio. They're off slightly.

The images aren't really a problem because it needs to be fixed anyways for the caption.

If you're seeing the 3rd item stuck underneath the 2nd item then it's because the 1st item is taller than the second. So the 3rd item is trying to go all the way to the left but gets blocked by that 1st item which extends down lower. The solution is to clear the floats for the 1st item in every row. This way it drops down below all the previous floats and is able to move all the way to the left.

In a two column layout the first item in every row would be 1st, 3rd, 5th, 7th,... or every odd item.

Add the following after your rule that sets the li width to 45%:

#gallery li:nth-child(odd) {
  clear: left;
}

Later in the responsive stage this will be turned into a 3 column layout and so a different :nth-child expression is needed for that. I'm only telling you this now so you can save it and you'll know what to do when you get there.

This will go into responsive.css:

#gallery li:nth-child(odd) {
    clear: none;
  }

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

Here we have to first remove the clear property on the odd items which we set earlier in "main.css" because that was only good for 2 columns and it will mess up the 3 column layout. Then we can go ahead and clear the correct items for a 3 column layout. In a 3 column layout, the 1st item in every row would be 1st, 4th, 7th,... :nth-child(3n + 1) is the expression that would select those items. It will select the 1st item and then every 3rd item after that.

I recommend you save this or bookmark this discussion so you can put this in when you get to the 3 column video.

Let me know if you have any questions about this.

Jacob Miller
Jacob Miller
12,466 Points

Well I looked at your code and didn't notice anything wrong... what size are your images?

Cheryl Hurley
Cheryl Hurley
2,250 Points

They range from 625x417 to 850x567, but all basically the same proportions. Here's the workspace preview if this helps..

http://web.6jldf78ajk.treehouse-app.com/

Jacob Miller
Jacob Miller
12,466 Points

That is weird that it's doing that. I tried some different things, but the only way I could get them to line up right was to add a height value to the list items. I think the slight differences the image proportions are messing it up and making each li a slightly different height, which is causing them to not line up correctly. I would try re-cropping all your images to the exact same width and height and see if that fixes it.

Cheryl Hurley
Cheryl Hurley
2,250 Points

Thanks for the extremely helpful and detailed answer! It obviously worked for this stage, and I'll be returning when we get into the responsive portion. Child pseudo-classes have never been my favorite, but they're making a lot more sense to me applied in this respect, actually. Thanks again.