The :nth-child()
CSS pseudo-class matches one or more elements based on their position among a group of siblings.
/* Selects every fourth element among any group of siblings */ :nth-child(4n) { color: lime; }
Syntax
The nth-child
pseudo-class is specified with a single argument, which represents the pattern for matching elements.
Keyword values
odd
- Represents elements whose numeric position in a series of siblings is odd: 1, 3, 5, etc.
even
- Represents elements whose numeric position in a series of siblings is even: 2, 4, 6, etc.
Functional notation
<An+B>
- Represents elements whose numeric position in a series of siblings matches the pattern
An+B
, for every positive integer or zero value ofn
. The index of the first element is1
. The valuesA
andB
must both be<integer>
s.
Formal syntax
:nth-child( <nth> [ of <selector># ]? )
Examples
Example selectors
tr:nth-child(odd)
ortr:nth-child(2n+1)
- Represents the odd rows of an HTML table: 1, 3, 5, etc.
tr:nth-child(even)
ortr:nth-child(2n)
- Represents the even rows of an HTML table: 2, 4, 6, etc.
:nth-child(7)
- Represents the seventh element.
:nth-child(5n)
- Represents elements 5, 10, 15, etc.
:nth-child(3n+4)
- Represents elements 4, 7, 10, 13, etc.
:nth-child(-n+3)
- Represents the first three elements among a group of siblings.
p:nth-child(n)
- Represents every
<p>
element among a group of siblings. This is the same as a simplep
selector. p:nth-child(1)
orp:nth-child(0n+1)
- Represents every
<p>
that is the first element among a group of siblings. This is the same as the:first-child
selector.
Detailed example
HTML
<h3><code>span:nth-child(2n+1)</code>, WITHOUT an <code><em></code> among the child elements.</h3> <p>Children 1, 3, 5, and 7 are selected.</p> <div class="first"> <span>Span 1!</span> <span>Span 2</span> <span>Span 3!</span> <span>Span 4</span> <span>Span 5!</span> <span>Span 6</span> <span>Span 7!</span> </div> <br> <h3><code>span:nth-child(2n+1)</code>, WITH an <code><em></code> among the child elements.</h3> <p>Children 1, 5, and 7 are selected.<br> 3 is used in the counting because it is a child, but it isn't selected because it isn't a <code><span></code>.</p> <div class="second"> <span>Span!</span> <span>Span</span> <em>This is an `em`.</em> <span>Span</span> <span>Span!</span> <span>Span</span> <span>Span!</span> <span>Span</span> </div> <br> <h3><code>span:nth-of-type(2n+1)</code>, WITH an <code><em></code> among the child elements.</h3> <p>Children 1, 4, 6, and 8 are selected.<br> 3 isn't used in the counting or selected because it is an <code><em></code>, not a <code><span></code>, and <code>nth-of-type</code> only selects children of that type. The <code><em></code> is completely skipped over and ignored.</p> <div class="third"> <span>Span!</span> <span>Span</span> <em>This is an `em`.</em> <span>Span!</span> <span>Span</span> <span>Span!</span> <span>Span</span> <span>Span!</span> </div>
CSS
html { font-family: sans-serif; } span, div em { padding: 5px; border: 1px solid green; display: inline-block; margin-bottom: 3px; } .first span:nth-child(2n+1), .second span:nth-child(2n+1), .third span:nth-of-type(2n+1) { background-color: lime; }
Result
Specifications
Specification | Status | Comment |
---|---|---|
Selectors Level 4 The definition of ':nth-child' in that specification. |
Working Draft | Adds of <selector> syntax and specifies that matching elements are not required to have a parent. |
Selectors Level 3 The definition of ':nth-child' in that specification. |
Recommendation | Initial definition. |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1 | (Yes) | 3.5 (1.9.1) | 9.0 | 9.5[1] | 3.1 |
of <selector> syntax |
No support | ? | No support[2] | ? | ? | ? |
No parent required | 57 | ? | 51 (51)[3] | ? | 44 | ? |
Feature | Android Webview | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | 1.0 (1.9.1)[2] | 9.0 | 9.5[1] | 3.1 |
of <selector> syntax |
No support | ? | ? | No support[2] | ? | ? | ? |
No parent required | 57 | 57 | ? | 51.0 (51)[3] | ? | 44 | ? |
[1] Opera can't handle dynamic insertion of elements.
[2] Gecko doesn't implement this feature yet. See bug 854148.
[3] See bug 1300374.
See also
Document Tags and Contributors
Tags:
Contributors to this page:
mfluehr,
scottpham,
chrisdavidmills,
yisibl,
Sebastianz,
wellset,
jpmedley,
PyroLagus,
erikadoyle,
corysimmons,
Konrud,
threehams,
_Romain_,
MusikAnimal,
kscarfone,
WGH,
FredB,
teoli,
Sheppy,
joyously,
trevorh,
dakill,
ethertank,
ronj,
McGurk,
Mkmelin,
miken32
Last updated by:
mfluehr,