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

Kailash Seshadri
Kailash Seshadri
3,087 Points

:nth-child

I cant seem to wrap my head around the :nth-child pseudo class.. can someone please it to me in simple words?

1 Answer

Steven Parker
Steven Parker
231,007 Points

In it's simple form (nth-child(number)) it's simply what order the item came in. For exmple, li:nth-child(3) would target only the third of a set of list items.

Then if you add a letter 'n' after the number, it repeats. An example of this might be li:nth-child(3) which would target every third item (3, 6, 9, 12, and so on).

Finally, if you add a plus sign (+) and another number, that number is the offset or starting number. For example, li:nth-child(3n+5) would target every third item starting with number 5 (5, 8, 11, 14, and so on).

Does that clear it up?

Kailash Seshadri
Kailash Seshadri
3,087 Points

So in simple words, p:nth-child (3) ,for example, will select the third p element in the html and p:nth-child (3n), will select the 3rd, 6th, 9th, 12th... paragraph elements in the html?