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 CSS Foundations Advanced Selectors Pseudo-Classes: :nth-child

nth-last-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 :)