References

Beginner-friendly references for web development, with live, editable examples.

The CSS :nth-child() selector

Selector CSS All modern browsers
In short

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

Formula Tester Ready

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

Live 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.

Quantity query (4+ items)

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;
}

: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) / even for zebra-striped rows and lists to aid readability.
  • Use an An+B formula like 3n+1 to 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

Baseline: Widely available
Across browsers since 2015 · of S since May 2023 W3C Selectors Level 4 (opens in new tab)

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?
Use tr:nth-child(even) (or odd) to stripe alternate rows for easier reading.
What does nth-child(3n+1) mean?
It matches every third element starting from the first: the 1st, 4th, 7th and so on. 3n alone matches the 3rd, 6th, 9th.
Why is my nth-child not matching?
It counts among all siblings, not just elements of one type. 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?
The 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?
Chain two :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.

Related tools