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 trialVoyti 1
1,854 PointsCan someone explain to me ":nth-child(4n)" syntax?
Does the "n" in :nth represent the same number as "n" in parentheses? Is nth-child(4n) is actually an equation where "-" represent minus?
3 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Wojti,
nth-child
is only the name that was chosen for this pseudo class. No numbers are going in for that 'n' and the hyphen is not a minus sign. It's only part of the name.
Figuring out which elements will be selected is determined by the expression inside the parentheses.
It takes the general form of an + b
. You can pick any integers you want for a
and b
n
takes on the values 0, 1, 2, 3, 4... in order to figure out which elements will be selected.
Using your 4n
example:
n | 4n
------
0 | 0
1 | 4
2 | 8
3 | 12
Child numbering always starts at 1. So in the second column of that table, you would ignore anything that's not positive. In this case we would ignore the 0 entry. From that, you can see that children 4, 8, 12... will be selected.
This is probably along the lines of how the browser figures this out. But substituting values of 'n' and making a table isn't very convenient for us if we want to quickly know what will be selected.
Here's how I read an + b
expressions. b
is what you start with and a
is what you keep adding.
Using 4n + 3
as an example, you start with 3 and you keep adding 4.
That gives us 3, 7, 11, 15...
Another example, -n + 5
(b is 5 and a is -1)
Start with 5 and then keep adding -1
5, 4, 3, 2, 1, 0, -1, -2, -3...
Since child numbering starts at 1 and goes up, we would discard the 0 and all those negative numbers which would give us the first 5 children.
Chris Jardine
13,299 PointsHi Wojti :nth-child(4n) Just means it will target every 4th nth-child. The example below will target paragraph <p>4</p> and <p>8</p> and will make the text red.
p:nth-child(4n) {
color: red;
}
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
Voyti 1
1,854 PointsBut does the "n" in ":nth" represent the same number as "n" in parentheses? Is nth-child(4n) is actually an equation where "-" represent minus? Or ":nth-child" is only the part of the syntax, and only the number in parentheses (like 4n) is what we look at to know which element is being selected? Also; can "n" be any number, as long as it's integer?
Voyti 1
1,854 PointsVoyti 1
1,854 PointsThank you very much, Jason. Now I get it