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

I was about confused as to how the :nth-last-child(-n+3) started counting up the number line.

Can someone help me?

1 Answer

Mark Lavar It is a simple.

nth-last-child() works as nth-child() but nth-last-child() starts its selection from bottom of the list. Meaning from last child of a parent.

Check the example please.

<ul>
  <li>First Item</li>
  <li>Second Item</li>
  <li>Third Item</li>
  <li>Fourth Item</li>
  <li>Fifth Item</li>
</ul>
li {
  background: slategrey;
}
/* select the second-last item */
li:nth-last-child(2) {
  background: lightslategrey;
}

-n + 3 equation selects Third Item, Fourth Item and Fifth Item in the list. Becasue 3 in the equation "-n + 3" says starts at third item and -n meaning -1 * n. Saying selects item from Third Item and below.

It will go down the list because it decreases.

If n is a + selectors will go up and selects everything from Third Item to First Item inclusive.

I hope i answered your question.

Yes it did. Thank you so much! I wasn't really sure. Now I can move on.