CSS :nth-child()
cheat sheet
Summary
The CSS :nth-child()
selector styles elements based on their order within a parent container, supporting exact positions, even/odd patterns, and mathematical formulas. It enables flexible, dynamic styling by targeting specific children in a list or collection.
Introduction #
The CSS :nth-child()
selector is a pseudo-class used to style specific elements based on their order within a parent container. It allows you to apply styles to elements that match a specified pattern.
The :nth-child()
selector counts elements based on their position within the parent, not based on class or type unless combined with the element's tag. Counting starts from 1
, not 0
.
Also see the CSS selectors cheat sheet.
Syntax: #
selector:nth-child(n)
selector
: The element, e.g.p
, you want to style.n
: A mathematical formula, number, or keyword that determines which child elements to match.
Specificity of :nth-child() #
The :nth-child()
pseudo-class is treated like a class selector, meaning it has a specificity value of 0, 1, 0
(zero for inline styles, one for classes/pseudo-classes, zero for element selectors).
:nth-child()
is treated like a class and has a specificity of0, 1, 0
.- Combining
:nth-child()
with IDs or other selectors increases specificity. - The highest specificity rule overrides lower-specificity rules.
- Inline styles (
style=""
) have the highest specificity (1, 0, 0, 0
).
Recipes #
Select the second child element #
li:nth-child(2) { background: black; color:white; }
Select only the first two child elements #
li:nth-child(-n+2) { background: black; color:white; }
Select all but the first two child elements #
li:nth-child(n+3) { background: black; color:white; }
Select every second, starting at the first child element #
li:nth-child(4n+1) { background: black; color:white; }
Select the odd child elements #
li:nth-child(odd) { background: black; color:white; }
Select the even child elements #
li:nth-child(even) { background: black; color:white; }
Select the last child element #
li:nth-last-child { background: black; color:white; }
Also: :last-child
selector
Select the third to last child element #
li:nth-last-child(3) { background: black; color:white; }
Select the last two child elements #
li:nth-last-child(-n+2) { background: black; color:white; }
Further readings #
Sources and recommended, further resources on the topic:
- W3C: Structural pseudo-classes
- MDN: :nth-child CSS selector
- MDN: CSS pseudo classes
- CSS selectors cheat sheet
License
License: CSS :nth-child()
cheat sheet by Jonas Jared Jacek is licensed under CC BY-NC-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. If others modify or adapt the material, they must license the modified material under identical terms. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/publications/css-n-th-child-cheat-sheet">CSS :nth-child()
cheat sheet</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-NC-SA 4.0</a>.</p>
For more information see the DITig legal page.