CSS :nth-child() Pseudo-Class

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The :nth-child() pseudo-class matches elements by their 1-based index among all sibling children of the same parent — any tag type counts in the position.

Use it for zebra stripes, “every third item,” or “the first three.” Example: li:nth-child(odd) styles items 1, 3, 5, ….

Baseline — widely available.

Core odd / even / An+B patterns are long-supported. The newer of <selector> form has narrower historical support — treat it as progressive when you need filtered counting.

CSS Syntax

syntax.css
:nth-child( <An+B> | even | odd)

Optional filter: :nth-child(even of .noted). Examples: :nth-child(3), :nth-child(2n), :nth-child(-n+3).

Pattern Values

Pass one pattern that describes which sibling indices to match:

PatternMatchesTry it
odd / 2n+11, 3, 5, …Try it
even / 2n2, 4, 6, …Try it
3 (number)Only the 3rd childTry it
-n+3First three childrenTry it
3n / n+4Every 3rd / from 4th onwardAn+B

How to Use :nth-child()

Count siblings from the parent, then style the matching positions.

  1. Put items under one parent (ul, tbody, …).
  2. Choose a pattern: odd, a number, or An+B.
  3. Write li:nth-child(pattern) (or tr, div, …).
  4. Remember: other tag types still occupy a child slot in the count.
PieceRequired?Role
Shared parentYesSiblings share one child list
PatternYesodd, even, number, or An+B
Element typeUsuallyMust also match (e.g. li) at that index
of filterOptionalCount only among a filtered subset

Live Preview

Change the pattern. Highlighted items are the ones that match li:nth-child(…).

Rule shown: li:nth-child(odd)

List
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Matching odd positions

CSS Examples

Short patterns that are easy to remember. Use Try It Yourself for a live editor.

Example 1 — Odd Rows (Zebra)

Highlight every odd list item.

CSS
<style>
  li:nth-child(odd) {
    background: #dbeafe;
  }
</style>

<ul>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
</ul>
Try It Yourself

How It Works

  1. Count siblings from 1. One=1, Two=2, Three=3, Four=4.
  2. odd matches 1 and 3. Same as 2n+1.
  3. Even items stay plain. Use even or 2n for the other stripe.
  4. Great for tables: tr:nth-child(odd) on tbody rows.

Example 2 — Exact Position

Style only the third child.

CSS
<style>
  li:nth-child(3) {
    color: #b91c1c;
    font-weight: 700;
  }
</style>

<ul>
  <li>A</li>
  <li>B</li>
  <li>C</li>
  <li>D</li>
</ul>
Try It Yourself

How It Works

  1. A number means one index. 3 is only the third child.
  2. Same as :nth-child(0n+3) or combining with :first-child when the index is 1.
  3. If a different tag sits at that slot, li:nth-child(3) does not match that element.
  4. Use for “feature the third card” without extra classes.

Example 3 — First Three (-n+3)

Match a range with an An+B formula.

CSS
<style>
  li:nth-child(-n+3) {
    border-left: 4px solid #2563eb;
  }
</style>

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>
Try It Yourself

How It Works

  1. Formula: for n = 0, 1, 2, … compute -n+3 → 3, 2, 1 (then 0, −1… ignored).
  2. Positive indices 1–3 match. Items 4 and 5 do not.
  3. n+4 would mean “from the 4th onward.”
  4. Handy for “top N” badges without markup changes.

Edge Cases & Pitfalls

Most confusion comes from counting all siblings, not only matching tags.

counting

Every sibling counts

A nested em or text wrapper still takes an index. Prefer :nth-of-type() when you only want same-tag positions.

index

Starts at 1, n starts at 0

Elements are 1-based. In An+B, n runs 0, 1, 2, … so 5n first hits 5 (not 0).

of

of changes what you count

:nth-child(2 of .noted) is the 2nd .noted sibling — not “the 2nd child if it has .noted.” Test support before relying on it.

hidden

Hidden rows still occupy a slot

Zebra stripes can look broken when a row is hidden. Filtered counting (even of :not([hidden])) can fix that where supported.

Browser Support

:nth-child() is a browser CSS pseudo-class. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

:nth-child()

odd, even, and An+B have been available across browsers for years (CSS3 Selectors). The of <selector> syntax is newer — check support if you need filtered counting. Internet Explorer 9+ supports the classic form.

98% Widely available
Google Chrome Supported
Full An+B · of modern
Mozilla Firefox Supported
Full An+B · of modern
Apple Safari Supported
Full An+B · of modern
Microsoft Edge Supported
Full An+B · of modern
Opera Supported
Full An+B
Samsung Internet Supported
Full An+B
Internet Explorer 9+
Classic An+B · no of
:nth-child() Baseline · Widely available

Bottom line: Safe for zebra stripes and An+B patterns everywhere modern. Use of selectively. This is not a JavaScript runtime API.

Key Takeaways

  • Role: select by sibling position (1-based) among all children.
  • Shortcuts: odd = 2n+1, even = 2n.
  • Start simple: li:nth-child(odd) { … }.
  • Same-tag counting? Use :nth-of-type() instead.

One line: :nth-child() paints by position in the sibling list — count carefully, style lightly.

Frequently Asked Questions

It matches elements by their 1-based position among all sibling children of the same parent. You pass odd, even, a number, or an An+B formula.
At 1. :nth-child(1) is the first child. The variable n in An+B starts at 0, but element indices still begin at 1.
:nth-child() counts every sibling regardless of tag. :nth-of-type() counts only siblings of the same element type.
Yes. odd and 2n+1 both match positions 1, 3, 5, …. even matches the same set as 2n.
You can write :nth-child(even of .noted) to count only among elements that match a filter. Support for of is newer than basic An+B; test when you need it.
No. It is a browser CSS selector. Node.js, Deno, and Bun do not apply it.

Next: :nth-last-child() Pseudo-Class

Continue through the CSS tutorial reference.

:nth-last-child() Pseudo-Class tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful