Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS CSS Foundations Advanced Selectors Pseudo-Classes: :nth-child


could someone please explain me how nth-last-child should be used. in this video he uses nth-last-child(-n +3) and for some reason the last 3 list items are selected. shouldn't it be like 3 represents the 8th list item and then coz its -n it should go one above till the first list item?

3 Answers

Stephan Kuster
Stephan Kuster
7,168 Points

what you are saying is nth-child(-n+8), 8 list item is selected and it will work his way back to the 1st list item. Now the nth-last-child does the same, but it starts at the 10th list item, sort of the first item for that selector. Making the n negative (-n) it will fill the list items towards the first list item (in the case of the nth-last-child: list item 10) I tried to make it a little more visible down below.

nth-child(-n+3) 1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10 nth-last-child(-n+3)

Hope I made some sense on my first answer :)

thanks a lot for the help and not bad for your first answer :)