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 trialSoufiane Bdaoui
9,602 Points:nth-child pseudo-class
Hi guys. I'm trying to understand how the :nth-child pseudo class works.
I can make it work with simple values and with keywords (odd / even). But, when i try to put a function, it just doesn't work.
This is the html:
<!DOCTYPE html>
<html>
<head>
<title>Structural Pseudo-classes</title>
<link rel="stylesheet" type="text/css" href="main.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>
This is the stylesheet:
li {
width: 100px;
height: 30px;
margin: 2px auto;
padding: 20px;
background: #ccc;
list-style: none;
font: 20px sans-serif;
color: tomato;
}
li:nth-child(1n4) {color: black;}
I do understand the theory behind that ":nth-child(1n4)". But no matter what, i can't have the effect that i'm trying to have ( color black to all the li from the 4th).
Instead, if i use those:
li:nth-child(5) {background: black;}
li:nth-child(even) {color: orange;}
It will work fine. I can say it even without refreshing my browser because sublime text change the font style form normal to italic when i type a pseudo-class.
What i'm doing wrong?
2 Answers
Paolo Scamardella
24,828 PointsIf I understood you correctly, you want to add color black from 4 to 7....I haven't done nth-child and css in a long time, but I believe this is what you are looking for: li:nth-child(1n+4)
Soufiane Bdaoui
9,602 PointsYa, the plus sign was the thing that made my code invalid. Thanks!