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

nth-child(#) should be nth-child(#n)?

There's already been a question about code challenge for Making a Website - Stage 8 "Responsive Web Design."

The code challenge is here: http://teamtreehouse.com/library/refactor-the-layout

My question is about the right answer to another question, here: https://teamtreehouse.com/forum/code-challenge-question-inside-the-media-query-clear-the-left-side-of-every-4th-list-item-in-the-gallery

The list items won't clear left unless you use the code "#gallery li:nth-child(4n) {..."

But the code during the lesson doesn't use "child(4n)" it uses "child(4)" (no "n" is used within the parentheses).

the wC3 doesn't use the nth-child(#n) format either. it just uses nth-child(#), as seen here: http://www.w3schools.com/cssref/sel_nth-child.asp

So why does this code challenge require the added "n," as in "nth-child(4n)" rather than "nth-child(4)"? Are these both valid syntax for different things? Is only the former valid but not the latter? Is there something quirky about the code challenge CSS file that demands this?

http://codepen.io/anon/pen/pvywJe

1 Answer

Hi Joseph,

li:nth-child(2) will select only the second child in a list of four items.

li:nth-child(2n) will select the second and fourth child in a list of four items.

Therefore you don't see the difference in the case of selecting the 4th child in a list of four.

It explains if you scroll further down the page http://www.w3schools.com/cssref/sel_nth-child.asp