CSS :nth-child() Selector

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 4 Examples
Positional

What You’ll Learn

The :nth-child() pseudo-class targets elements by their position among siblings. It powers zebra stripes, grid patterns, and precise positional styling.

01

By position

1-based index.

02

odd / even

Striped rows.

03

2n, 3n+1

Formulas.

04

li:nth-child

List patterns.

05

Tables

Row striping.

06

vs nth-of-type

Know the diff.

Introduction

The :nth-child() selector in CSS allows you to target elements based on their position within a parent element. This pseudo-class is extremely flexible, letting you style elements based on patterns or specific positions.

Whether you are working with tables, lists, or any repeated HTML structure, :nth-child() is a powerful tool for dynamic styling.

Definition and Usage

Write element:nth-child(n) where n is a number, the keyword odd or even, or a formula like 2n+1.

💡
Beginner Tip

Counting starts at 1, not 0. :nth-child(1) is the first child. :nth-child(odd) and :nth-child(2n+1) select the same rows — items 1, 3, 5, and so on.

📝 Syntax

The syntax for the :nth-child() pseudo-class is:

syntax.css
element:nth-child(n) {
  /* CSS properties */
}

element is the tag you want to style. n can be a number, keyword, or an+b formula.

Common Patterns

ExpressionMatches children at positions…
:nth-child(2)2 (second child only)
:nth-child(odd)1, 3, 5, 7, …
:nth-child(even)2, 4, 6, 8, …
:nth-child(2n)2, 4, 6, 8, … (same as even)
:nth-child(2n+1)1, 3, 5, 7, … (same as odd)
:nth-child(3n+1)1, 4, 7, 10, …

The an+b Formula

  • n is a counter starting at 0, increasing by 1 for each child.
  • a is the step size — how often to select (e.g. every 2nd, every 3rd).
  • b is the offset — the starting position in the sequence.
li:nth-child(odd) li:nth-child(2n) li:nth-child(3) li:nth-child(3n+1)

Related Selectors

  • :nth-of-type() — counts only siblings of the same tag type
  • :first-child / :last-child — shorthand for positions 1 and last
  • :nth-last-child() — counts from the end of the sibling list
  • :not() — combine for exclusions like li:not(:nth-child(1))

⚡ Quick Reference

QuestionAnswer
Selector typeFunctional structural pseudo-class
Syntaxelement:nth-child(n)
Index starts at1 (not 0)
CountsAll sibling children (any tag type)
Common useZebra stripes, highlight 3rd item, grid patterns
Browser supportAll modern browsers

When to Use :nth-child()

:nth-child() is ideal for position-based patterns:

  • Zebra striping — Alternate row colors with :nth-child(odd) or :nth-child(even).
  • Highlight a position — Style the third item with :nth-child(3).
  • Repeating patterns — Every third card with :nth-child(3n+1).
  • Table rowstr:nth-child(even) for readable data tables.
  • Grid layouts — Clear every 4th item in a flex or grid row pattern.

👀 Live Preview

Odd items get a blue background; the 3rd item is bold red via :nth-child():

  • Item 1
  • Item 2
  • Item 3
  • Item 4
  • Item 5

Examples Gallery

Practice :nth-child() with zebra stripes, specific positions, formulas, and table row styling.

📜 Core Patterns

Style elements by position and repeating patterns.

Example 1 — Zebra stripes on even rows

Give every second list item a light blue background using li:nth-child(2n).

nth-child-zebra.css
li:nth-child(2n) {
  background: #dbeafe;
}

li:nth-child(3) {
  font-weight: 700;
  color: #b91c1c;
}
Try It Yourself

How It Works

2n matches multiples of 2 (items 2 and 4). The separate :nth-child(3) rule highlights the third item regardless of the stripe pattern.

Example 2 — Style odd-numbered items

Use the odd keyword for a readable alternating background on odd rows.

nth-child-odd.css
li:nth-child(odd) {
  background: #f1f5f9;
}

/* Equivalent to :nth-child(2n+1) */
Try It Yourself

How It Works

odd is shorthand for every odd-positioned child: 1, 3, 5, 7, and so on. It is identical to 2n+1.

📄 Formulas & Tables

Use an+b expressions and apply patterns to table rows.

Example 3 — Every third item starting at 1

The 3n+1 formula selects items 1, 4, 7, 10, and so on.

nth-child-formula.css
li:nth-child(3n+1) {
  background: #dcfce7;
  border-left: 4px solid #16a34a;
  padding-left: 0.65rem;
}
Try It Yourself

How It Works

When n=0, the formula gives 1. When n=1, it gives 4. When n=2, it gives 7. The step a=3 skips two items between each match.

Example 4 — Zebra stripes on table rows

Apply alternating backgrounds to tr elements for readable data tables.

nth-child-table.css
tr:nth-child(even) {
  background: #f8fafc;
}

tr:nth-child(1) {
  font-weight: 700;
  background: #1e293b;
  color: #fff;
}
Try It Yourself

How It Works

The first row gets header styling via :nth-child(1). Even data rows get a subtle gray background for zebra striping.

💬 Usage Tips

  • Use odd/even keywords — More readable than 2n+1 for simple striping.
  • Combine selectorsul li:nth-child(2) scopes to list items inside a ul.
  • Test formulas — Plug n=0,1,2 into an+b to verify which positions match.
  • Tables and gridstr:nth-child(even) is the classic zebra table pattern.
  • When type matters — Switch to :nth-of-type() if mixed tags break your count.

⚠️ Common Pitfalls

  • Counts all children — A <div> before your <li> shifts the count; use :nth-of-type() if needed.
  • 1-based, not 0-based — The first child is :nth-child(1), not 0.
  • Complex formulas — Double-check a (step) and b (offset) in an+b expressions.
  • p:nth-child(2) trap — Matches a p only if it is the second child overall, not the second paragraph.
  • Overlapping rules — Multiple :nth-child() rules can apply to the same element; watch specificity.

♿ Accessibility

  • Contrast on stripes — Alternating row colors must still meet WCAG contrast requirements.
  • Do not rely on color alone — Pair zebra stripes with clear borders or labels where needed.
  • Semantic tables — Use <th> for headers; :nth-child(1) on tr is not a substitute.
  • Lists stay lists — Use proper <ul>/<ol> markup for screen readers.

🧠 How :nth-child() Works

1

Browser lists siblings

All children inside the parent are numbered starting from 1.

Index
2

Evaluates n expression

The value inside :nth-child() is tested against each position.

Match
3

Filters by element

Only elements matching the tag before :nth-child() receive styles.

Filter
=

Pattern-based styling

Stripes, highlights, and grid rhythms appear without extra HTML classes.

🖥 Browser Compatibility

:nth-child() is supported in all modern browsers and has been available since CSS3 Selectors.

Baseline · Universal support

Positional selectors everywhere

:nth-child() works reliably in Chrome, Firefox, Safari, Edge, and Opera.

99% Universal support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
:nth-child() pseudo-class 99% supported

Bottom line: :nth-child() is safe for lists, tables, and layout patterns in all modern projects.

🎉 Conclusion

The CSS :nth-child() selector provides a robust way to style elements based on their position within a parent container. It enables dynamic, patterned styling that improves maintainability.

From simple zebra stripes to complex an+b formulas, mastering :nth-child() helps you handle lists, tables, and grids with ease.

💡 Best Practices

✅ Do

  • Use odd/even for simple zebra striping
  • Test an+b with n=0,1,2 before deploying
  • Scope with parent selectors like ul li:nth-child()
  • Use :nth-of-type() when mixed tags break counts
  • Combine with :not() for refined patterns

❌ Don’t

  • Assume counting starts at 0
  • Expect p:nth-child(2) to mean second paragraph
  • Forget that all sibling types are counted
  • Overcomplicate when odd/even suffices
  • Rely on stripe color alone for meaning

Key Takeaways

Knowledge Unlocked

Five things to remember about :nth-child()

Use these points when styling by position.

5
Core concepts
odd 02

Keywords

odd / even.

Syntax
2n 03

Formulas

an+b.

Pattern
04

vs nth-of-type

All children.

Compare
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

The :nth-child() pseudo-class matches elements based on their position among all sibling children inside a parent. You pass a number, odd, even, or an an+b formula inside the parentheses.
Counting starts at 1, not 0. :nth-child(1) matches the first child, :nth-child(2) the second, and so on.
They select the same elements — odd-numbered children (1, 3, 5, …). :nth-child(even) is equivalent to :nth-child(2n).
:nth-child() counts all sibling children regardless of tag type. :nth-of-type() counts only siblings of the same element type.
Yes. All modern browsers support :nth-child(). It has been available since CSS3 Selectors and works reliably for lists, tables, and grids.

Practice in the Live Editor

Open the HTML editor and experiment with :nth-child(), zebra stripes, and an+b formulas.

HTML Editor →

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