CSS Pseudo-class :nth-child(an+b)
Selects every nth child-element by using the formula an+b. a and b are integers. If you want to select every fourth child-element of body
starting with the first child, you write body > *:nth-child(4n+1)
. This will select the first, the fifth, the ninth (and so on) child-element of body
. If you want to select the fourth, the eighth, the twelfth and so on, you just write body > *:nth-child(4n)
. You also can use negative numbers for b. body *:nth-child(4n-1)
will select the third, seventh, eleventh, fifteenth... child-element of body
.
Instead of an+b you also can use two keywords: even and odd which are equal to 2n and 2n+1.
Examples
Compatibility
Chrome | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|
All | All | 9+ | 9.5+ | 3.2+ |