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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Selector for the first 4 items in a list.

Hello you wonderful treehouse people. For the first time, I'm stumped by a challenge. I paid attention to the video, honest ;)

Could you tell me the selector needed to target only the first 4 items of a list? Thanks. :)

1 Answer

li:nth-child(-n+4) {
 font-weight: bold;
}

The best explanation I've seen is:

"Using "-n" values seems a little weird, because if the end result is negative there is no match, so you'll need to add to the equation to get it back positive again. As it turns out, this is a rather clever technique. You can use it to select the "first n elements" with "-n+3":

-0 + 3 = 3 = 3rd Element -1 + 3 = 2 = 2nd Element -2 + 3 = 1 = 1st Element -3 + 3 = 0 = no match"

from - http://css-tricks.com/how-nth-child-works/