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 Responsive Web Design and Testing Refactor the Layout

syazana zainuddin
syazana zainuddin
639 Points

Hello, I am lost. How to clear left side of every 4th list item in the gallery?

Inside the media query, clear leftside of every 4th list item in the gallery.

3 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi, You will want to select the every 4th item with the nth-child selector.

@media screen and (min-width: 480px) {
  #gallery li {
    width: 28.3333%;
  }
  #gallery li:nth-child(4n) {
    clear: left;
}

If you are unsure how it came to be 4n, I do suggest you review/watch this Treehouse Video

Keep Coding! :)

I actually think Nick may be mistaken about having to clear every 4th list item, and although it works the same way with just 2 rows, here's what I think the code should be:

@media screen and (min-width: 480px) {
  #gallery li {
    width: 28.3333%;
  }
  #gallery li:nth-child(3n+4) {
    clear: left;
  }
}

Using 3n+4 instead of 4n will clear every 3rd list item starting with the 4th (i.e. the 4th, 7th, 10th, and so on list items). As the gallery has 3 images per row, I think using 3n+4 is a more suitable choice than 4n.

To select every 4th list item, you would need to use a pseudo selector such as nth-child().

If you look in the code for the previous video, you will find an example of nth-child() being used.

Hope this helps :)

EDIT: removed "The challenge is a bit strange as it only accepts 1 answer but I think there is more than 1 way to achieve the same result." - I was using nth-child(4) and while the result looks the same its not what the question is asking - see Jason's post