The :nth-child
selector allows you to select one or more elements based on their source order, according to a formula. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling elements.
Suppose we are building a CSS grid, and want to remove the margin on every fourth grid module:
<section class="grid"> <article class="module">One</article> <article class="module">Two</article> <article class="module">Three</article> <article class="module">Four</article> <article class="module">Five</article> </section>
Rather than adding a class to every fourth item (e.g. .last), we can use :nth-child:
.module:nth-child(4n) { margin-right: 0; }
As you can see, :nth-child
takes an argument: this can be a single integer, the keywords “even” or “odd”, or a formula. If an integer is specified only one element is selected—but the keywords or a formula will iterate through all the children of the parent element and select matching elements — similar to navigating items in a JavaScript array. Keywords “even” and “odd” are straightforward (2, 4, 6 etc or 1, 3, 5 respectively). The formula is constructed using the syntax an+b
, where:
- “a” is an integer value
- “n” is the literal letter “n”
- “+” is an operator and may be either “+” or “-”
- “b” is an integer and is required if an operator is included in the formula
It is important to note that this formula is an equation, and iterates through each sibling element, determining which will be selected. The “n” part of the formula, if included, represents a set of increasing positive integers (just like iterating through an array). In our above example, we selected every fourth element with the formula 4n
, which worked because every time an element was checked, “n” increased by one (4×0, 4×1, 4×2, 4×3, etc). If an element’s order matches the result of the equation, it gets selected (4, 8, 12, etc). For a more in-depth explanation of the math involved, please read this article.
To illustrate further, here are some examples of valid :nth-child
selectors:
<div class="one"> <ul> <li>One</li> <li>Two</li> <li>Three</li> <li>Four</li> </ul> <ol> <li>Five</li> <li>Six</li> <li>Seven</li> <li>Eight</li> </ol> <i>select <li> elements that are the first child of their parent</i> </div> <div class="two"> <ul> <li>One</li> <li>Two</li> <li>Three</li> <li>Four</li> </ul> <ol> <li>Five</li> <li>Six</li> <li>Seven</li> <li>Eight</li> </ol> <i>select the third child element of the second element</i> </div> <div class="three"> <ul> <li>One</li> <li>Two</li> <li>Three</li> <li>Four</li> </ul> <ol> <li>Five</li> <li>Six</li> <li>Seven</li> <li>Eight</li> </ol> <ul> <li>Nine</li> <li>Ten</li> <li>Eleven</li> <li>Twelve</li> </ul> <i>select the first three <li> elements inside of every odd element</i> </div>
body { padding: 1em 2em; } ul, ol { list-style: none; padding: 0; } li { text-align: center; line-height: 2; background: slategrey; } div { width: 12em; float: left; margin-right: 2em; } hr { clear: both; padding-top: 1em; border: 0; border-bottom: 1px solid grey; } .one li:nth-child(1) { background: lightsteelblue; } .two :nth-child(2) :nth-child(3) { background: lightsteelblue; } .three :nth-child(odd) li:nth-child(-n+3) { background: lightsteelblue; } div:before { font-family: monospace; white-space: nowrap; font-size: 12px; } .one:before { content: "li:nth-child(1)"; } .two:before { content: ":nth-child(2) :nth-child(3)"; } .three:before { content: ":nth-child(odd) li:nth-child(-n+3)"; }
Points of Interest
:nth-child
iterates through elements starting from the top of the source order. The only difference between it and:nth-last-child
is that the latter iterates through elements starting from the bottom of the source order.- The
:nth-child
selector is very similar to:nth-of-type
but with one critical difference: it is less specific. In our example above they would produce the same result because we are iterating over only.module
elements, but if we were iterating over a more complex group of siblings,:nth-child
would try to match all siblings, not only siblings of the same element type. This reveals the power of:nth-child
—it can select any sibling element in an arrangement, not only elements that are specified before the colon.
Related Properties
Browser Support
Chrome | Safari | Firefox | Opera | IE | Android | iOS |
---|---|---|---|---|---|---|
Any | 3.2+ | Any | 9.5+ | 9+ | Any | Any |
:nth-child was introduced in CSS Selectors Module 3, which means old versions of browsers do not support it. However, modern browser support is impeccable, and the new pseudo-selectors are widely used in production environments. If you require older browser support, either polyfill for IE, or use these selectors in non-critical ways á la progressive enhancement, which is recommended.
Leave a Reply