The CSS :nth-child() selector
The CSS :nth-child() pseudo-class selects elements by their position among their siblings. Use keywords like odd and even for zebra-striped rows, or an An+B formula like 3n (every third) or 3n+1 (the 1st, 4th, 7th…). It counts among all siblings, which sometimes surprises people.
Overview
:nth-child() picks elements based on where they sit among their siblings. The two keywords cover the common cases: :nth-child(odd) and :nth-child(even) are how you stripe table rows or lists for readability.
For anything more, it takes an An+B formula. :nth-child(3n) matches every third element; :nth-child(3n+1) matches the 1st, 4th, 7th and so on, useful for styling the start of each row in a grid. You can also pass a plain number, :nth-child(2), to target one specific position.
The thing to watch is that it counts among all the siblings, regardless of type. p:nth-child(2) means "an element that is a <p> and is the second child"; if the second child is not a paragraph, nothing matches. When you want to count only elements of a given type, reach for :nth-of-type() instead. Related shortcuts :first-child and :last-child handle the ends.
Syntax
/* zebra striping */
tr:nth-child(even) {
background: #f5f7fb;
}
/* every third item */
.grid > :nth-child(3n) { … }
Try it: :nth-child() formula tester
Test any An+B formula, click tiles to toggle .featured for of S filtering, or switch to Mixed tags mode to compare :nth-child() against :nth-of-type().
Example
<style>
.rows div { padding: 10px 14px; font: 15px system-ui, sans-serif; }
.rows div:nth-child(odd) { background: #eef2ff; }
.rows div:nth-child(even) { background: #fff; }
</style>
<div class="rows">
<div>Row 1</div><div>Row 2</div><div>Row 3</div><div>Row 4</div>
</div>
Common selector recipes
Tested patterns for quantity queries, ranges, multi-column grids, and filtered tables.
Match a list only when it contains at least four items by combining :first-child with :nth-last-child(n+4).
/* Compact tag pills only when 4 or more exist */
.tags li:first-child:nth-last-child(n+4),
.tags li:first-child:nth-last-child(n+4) ~ li {
font-size: 0.85rem;
padding: 2px 8px;
}
Negative A (-n+3) stops after 3 because n starts at 0 (3, 2, 1) and zero or negative positions are ignored.
/* First 3 items */
.list > li:nth-child(-n+3) {
font-weight: 600;
}
/* Items 4 through 6 (range) */
.list > li:nth-child(n+4):nth-child(-n+6) {
border-left: 3px solid #1565c0;
}
Target the last card (3n) or first card (3n+1) in every 3-column row.
/* Last card in each 3-column row */
.cards > .card:nth-child(3n) {
border-right: none;
}
/* First card in each row */
.cards > .card:nth-child(3n+1) {
border-left-color: #1565c0;
}
Scope to tbody tr so thead header rows are not striped. Specificity: :nth-child(An+B of S) adds the highest specificity inside S on top of (0, 1, 0).
/* Specificity: (0, 2, 2) due to :not([hidden]) */
tbody tr:nth-child(even of :not([hidden])) {
background-color: #eef5ff;
}
:nth-child() vs :nth-of-type() vs :nth-last-child()
Most :nth-child() surprises happen when a parent mixes tag names (such as an <h2> before a group of <p> elements).
| Selector | Counts | Skips tags | of S filter | Best fit |
|---|---|---|---|---|
:nth-child(An+B) |
All siblings from start | No | Yes (May 2023) | Uniform lists, table rows, and grids |
:nth-child(An+B of S) |
Siblings matching S |
Yes (by S) |
Built in (May 2023) | Zebra rows skipping [hidden] |
:nth-of-type(An+B) |
Same-tag siblings | Yes (by tag) | No | Mixed <h2>, <figure>, and <p> |
:nth-last-child(An+B) |
All siblings from end | No | Yes (May 2023) | Last N (-n+3) and quantity queries |
Best practices
- Use
:nth-child(odd)/evenfor zebra-striped rows and lists to aid readability. - Use an
An+Bformula like3n+1to target a repeating position, such as the first item of each row. - Remember it counts all siblings; switch to
:nth-of-type()when you mean "the nth element of this type". - For the ends, the simpler :first-child and :last-child are clearer.
Browser compatibility
Core :nth-child(An+B) works in every browser. The CSS Selectors Level 4 of S filter reached cross-browser support in May 2023 and is widely available.
| Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
:nth-child(An+B), odd, even |
ChromeSupported since 1+ | EdgeSupported since 12+ | FirefoxSupported since 3.5+ | SafariSupported since 3.1+ |
:nth-child(An+B of S)Filters sibling pool before counting; adds S specificity |
ChromeSupported since 111+ | EdgeSupported since 111+ | FirefoxSupported since 113+ | SafariSupported since 9+ |
Frequently asked questions
How do I style every other row in CSS?
tr:nth-child(even) (or odd) to stripe alternate rows for easier reading.What does nth-child(3n+1) mean?
3n alone matches the 3rd, 6th, 9th.Why is my nth-child not matching?
p:nth-child(2) needs the 2nd child to be a paragraph. Use :nth-of-type() to count by type.What is the difference between nth-child and nth-of-type?
:nth-child() counts position among all siblings; :nth-of-type() counts position among siblings of the same element type.How does :nth-child(An+B of S) work, and how does it affect specificity?
of S clause filters siblings to only those matching selector S before counting An+B, so tr:nth-child(even of :not([hidden])) keeps stripes intact when rows are hidden. Its specificity is (0, 1, 0) plus the highest specificity in S (for example, :nth-child(even of .row) has (0, 2, 0) and overrides a plain .active class).How do I select a range of elements like items 4 through 6?
:nth-child() selectors together: li:nth-child(n+4):nth-child(-n+6). The first selects item 4 onward, and the second caps the match at item 6.






















