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

How many times can I use nth-child in my CSS?

https://w.trhou.se/hddzxo9zeh

Link to my html/css is above. Discussing my main.css and responsive.css portfolio portions.

I had the same issue as the tutorial in my mobile version of the site, when the screen width was reduced to minimum. I used #gallery li:nth-child(2n+1) {clear: left;} in my main.css, but now the #gallery li:nth-child(3n+1) {clear: left;} is no longer working for my 3 column portfolio page in responsive.css. Instead, I am getting a really jacked up 2 column version even at larger screen widths.

I dont think my CSS is cascading properly. HELP! :)

2 Answers

Bryce Santos
Bryce Santos
11,157 Points

I played around with your code and found that taking out the #gallery li:nth-child(2n+1) {clear: left;} from main.css fixed it.

Hariidaran Tamilmaran
seal-mask
.a{fill-rule:evenodd;}techdegree
Hariidaran Tamilmaran
iOS Development Techdegree Student 19,305 Points

Yeah, I found out what your problem was. In main.css, you typed:

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

And in responsive.css:

@media screen and (min-width: 480px) { /* line 28 */
  #gallery li:nth-child(3n+1) {
      clear: left;
  }
}

I don't understand why you typed in whatever I mentioned in responsive.css (it's not needed; you can remove it), but here is the explanation for whatever you typed in main.css:

I may be wrong, but the float property makes the elements after a specific element, get attracted to the specific element. And what the clear property does, is let any element under the influence of the float property of the specific element, be uninfluenced or stay where ever it is.

For example, the 3rd list item in #gallery is under the influence of the float property of the 2nd list item. So you put the clear: left for every odd list item. So you just have to remove whatever I mentioned above.

You may or may not have understood what I was talking about. So here are some helpful links:

Hmm... I guess I'll learn more detail as I move along the track. I was really hoping i can fix the issue though.

In my responsive.css file, I wanted to change my portfolio gallery to a 3 column layout, as opposed to the original 2 columns.. The reason why nth-child was placed as 3n+1 in responsive.css, is because the columns were not lining up properly. I followed the tutorial and it fixed the issue in responsive, however when I reduced the screen size to the lowest that my browser would allow, the 2 column layout of the gallery was not lined up properly.

Hariidaran Tamilmaran
seal-mask
.a{fill-rule:evenodd;}techdegree
Hariidaran Tamilmaran
iOS Development Techdegree Student 19,305 Points

The reason Nick decided to make the gallery as a two-item layout on smaller devices because three pictures gives the website's visitor a smaller scale compared to the two-item layout. Check out these courses: