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

Create a new rule that targets the first 4 list items only. Set the font weight to bold.

No clue what I am doing wrong here:

li:nth-child(even) { color: white; background: black; }

li:nth-child(3) { background: #FF6347; }

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

This works also:

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

6 Answers

Hard to say what you have going on, so I'm going to give you some resources.

First, read this: http://css-tricks.com/how-nth-child-works/

Than, play with this: http://css-tricks.com/examples/nth-child-tester/

Than, look at this cheatsheet http://css-tricks.com/useful-nth-child-recipies/

And finally, here is how to select the first four list items only http://codepen.io/kevink/pen/ayrtu

Speaking of nth-child resources

Always good to have these resources handy!

I also made http://codepen.io/jamesbarnett/full/Fanbj it started out as an nth-child playground to help me work out one and it morphed into this.

/* Write your CSS below */
li:nth-child(even) {
    color: white;
    background-color: black;
}

li:nth-child(3) {
    background-color: #FF6347;
}

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

(-n+4) should select the first four. See the codepen

Thanks!

I was able to fix it. I needed (1n+4) I believe for the last selection.

It should be noted that n+4 is the same as 1n+4

That sounds like it is doing the opposite of what you want. That's selecting every element after the fourth list item.

I always get that confused lol! Thank you for your response :)

If (-1n+4) is the same as (-n+4) then why is the latter not working? In fact the code changes into a strange green colour.