The idea of this answer is only to complement what was said by @bacco, but with a few more examples and use cases. In addition to making some clarifications of other points not addressed.
According to the official W3C documentation n it can be an integer (positive, negative, or 0). And the index of the first child of an element is 1
. Logo :nth-child(0n + 0.1)
is not valid Source: link
The% w_C% according to W3C would be (a n + b) , so% w_C% determines the size of the group and% w_% index, Formula
.
:nth-child(5n)
Represents the number elements [5 × 1] = 5 , [5 × 2] 10 , [5 × 3] = 15 , etc.
:nth-child(3n+4)
Representstheelementsofnumber[(3×0)+4]=4,[(3×1)+4]=7,[(3×2)+4]=10,[(3×3)+4]=13,etc.
:nth-child(-n+3)
Representsforwardthelastthreeelements.[=-0+3,-1+3,-2+3]
:nth-child(odd)ou:nth-child(2n+1)
RepresentstheoddrowsofanHTMLtable:1,3,5,etc.
:nth-child(even)ou:nth-child(2n)
RepresentstheevenlinesofanHTMLtable:2,4,6,etc.
nth-child(n)
Representsallchildelements
Tounderstandbetter,seetheimagesbelowwithadifferentapproach:
Blocks2bygrabbingindex1
Blocksof3bygrabbingindex2
Source: link
If the value of an
is b
, the corresponding element in the group will be ex: (3n+3) = ((3xn)+3)
index , but counted back to the front of index 1. In that case, element correspondent of a group will no longer correspond to an element of that group, but to one above it.
Using b
you can apply the same rules, but in this way the count index starts from forward.
Last-child Blocks of 3, but starting from the forward negativo
not bth
I'llstressthatusingnth-last-child
youcanhavethesameresults,butusingdifferent"rules". As can be seen below, all rules result in stylizing only the third element.
.foo span:nth-child(0n+3) {
background-color: blue;
}
.bar span:nth-child(+3) {
background-color: blue;
}
.foobar span:nth-child(3) {
background-color: blue;
}
span {
color:white;
display:inline-block;
padding:5px;
margin: 5px;
background-color:#f00
}
<div class="foo">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>9</span>
<span>10</span>
</div>
<div class="bar">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>9</span>
<span>10</span>
</div>
<div class="foobar">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>9</span>
<span>10</span>
</div>
Another use case would be to style only one element n forward, for example to split a list from half to front. Ex: in a list of 10 elements you would use 321
to stylize from the sixth element to the end of the list.
.foo span:nth-child(n+6) {
background-color: blue;
}
span {
color:white;
display:inline-block;
padding:5px;
margin: 5px;
background-color:#f00
}
<div class="foo">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>9</span>
<span>10</span>
</div>
Another use case for example is when you want to stop applying the style in the first and last child. You can concatenate two 123
to only stylize the "kernel" and leave the first and last child without the style:
.foo span:nth-child(n+2):nth-last-child(n+2) {
background-color: blue;
}
span {
color:white;
display:inline-block;
padding:5px;
margin: 5px;
background-color:#f00
}
<div class="foo">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>9</span>
<span>10</span>
</div>
And if you want to be even more specific and get the elements only from 4 to 8 you can concatenate two nth-child
to make a n+6
where you only get whatever is between the fourth and eighth child. Here's how it looks in the example below:
.foo span:nth-child(n+4):nth-child(-n+8) {
background-color: blue;
}
span {
color:white;
display:inline-block;
padding:5px;
margin: 5px;
background-color:#f00
}
<div class="foo">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>9</span>
<span>10</span>
</div>
But calm down, you can still be more specific! For example delete child 6 from this range, you should concatenate everything with a pseudo-classes
rule at the end, excluding nth-child
from the unwanted child, see the example below:
.foo span:nth-child(n+4):nth-child(-n+8):not(:nth-child(6)) {
background-color: blue;
}
span {
color:white;
display:inline-block;
padding:5px;
margin: 5px;
background-color:#f00
}
<div class="foo">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>9</span>
<span>10</span>
</div>
The formatting of the style should also be done carefully since range
can break your rule:
Valid examples with blanks
:nth-child( 3n + 1 )
:nth-child( +3n - 2 )
:nth-child( -n+ 6)
:nth-child( +6 )
Examples Invalid with whitespace
:nth-child(3 n)
:nth-child(+ 2n)
:nth-child(+ 2)