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

Oliver Sewell
Oliver Sewell
14,724 Points

create a new rules that targets the last 3 elements in the list nth child quiz

what have i done wrong ? T__T

index.html
<!DOCTYPE html>
<html>
<head>
    <title>Structural Pseudo-classes</title>
    <link rel="stylesheet" type="text/css" href="cc1-main-style.css">
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <ul>
        <li>List item 1</li>
        <li>List item 2</li>
        <li>List item 3</li>
        <li>List item 4</li>
        <li>List item 5</li>
        <li>List item 6</li>
        <li>List item 7</li>
    </ul>
</body>
</html>
style.css
/* Write your CSS below */
li:nth-child(even) {
  color: white;
  background: black;
}

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

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

li:nth-lastchild(-n+3) {
  font-size: 30px;
}

2 Answers

First you have to set just the color and background of all the <li> from <ul> elements So your code should look like this in the css file:

ul li {color: white; background-color: black;}

Then you have to set the third child background to the value 'FF6347':

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

Then set the first 4 elements to font-weight: bold

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

And finally change the last 3 elements font size:

ul li:nth-child(n+3){font-size: 30px;}

You can check this link for more useful information: https://css-tricks.com/useful-nth-child-recipies/

Oliver Sewell
Oliver Sewell
14,724 Points

Thankyou so why do we use ul li:nth-child(n+3){font-size: 30px;} and not li:nth-lastchild ?

I prefer to use it that way but will work perfectly well this way too "ul li:nth-last-child(-n+3)"

Keep coding