jQuery :nth-child() Selector

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Child index

What You’ll Learn

The :nth-child() selector picks elements by their position among siblings inside a parent — first, second, every even row, or every third item. Available since jQuery 1.1.4; counting is 1-based (unlike .eq()). Master fixed indexes, even/odd, and An+B equations.

01

Syntax

:nth-child(n)

02

1-based

First = 1

03

even/odd

Zebra stripes

04

An+B

3n, 3n+1

05

Official

li:nth-child(2)

06

vs .eq()

Parent-aware

Introduction

Lists, tables, and grids often need pattern styling — highlight the second menu item, stripe table rows, or mark every third card. CSS solved this with :nth-child(), and jQuery exposes the same selector since version 1.1.4.

The key idea: jQuery checks each element’s child index within its parent, counting all sibling elements (not just matching tags) starting at 1. That is different from jQuery’s .eq(), which uses 0-based indexing on whatever collection you already selected. Official docs stress this distinction — confusing the two is a common beginner mistake.

Understanding the :nth-child() Selector

Read li:nth-child(2) as: “Select every li that is also the second child of its parent.” If a div sits before the second li, that list item will not match — because it is the third child overall.

  • li:nth-child(1) → same as :first-child.
  • tr:nth-child(even) → even-position rows inside each tbody.
  • li:nth-child(3n) → every 3rd item: 3, 6, 9…
  • li:nth-child(3n+1) → 1, 4, 7, 10…
💡
Beginner Tip

When a selector “does nothing,” check sibling order. A hidden span or comment node before your target can shift the child index. Use browser DevTools to count children from 1.

📝 Syntax

Official jQuery API form (since 1.1.4):

jQuery
jQuery( ":nth-child(index/even/odd/equation)" )

// fixed index (1-based):
$( "ul li:nth-child(2)" )

// keywords:
$( "tr:nth-child(even)" )
$( "tr:nth-child(odd)" )

// An+B equations:
$( "li:nth-child(3n)" )      // 3, 6, 9 …
$( "li:nth-child(3n+1)" )    // 1, 4, 7 …
$( "li:nth-child(3n+2)" )    // 2, 5, 8 …

Parameters

  • index — a positive integer (1, 2, 3…), the keyword even or odd, or an An+B equation accepted by CSS.

Return value

  • A jQuery object containing every element that matches both the base selector and the nth-child rule within its parent.
  • Can match multiple elements — one per parent when lists repeat.

Official jQuery API example

jQuery
// Find the second li in each matched ul and note it
$( "ul li:nth-child(2)" ).append( " - 2nd! " );

⚡ Quick Reference

GoalSelector
First childli:nth-child(1) (= :first-child)
Second childli:nth-child(2)
Even rowstr:nth-child(even)
Every 3rd itemli:nth-child(3n)
1st of each group of 3li:nth-child(3n+1)

📋 :nth-child() vs .eq() and :even

Official jQuery docs highlight three different indexing models — do not mix them up.

:nth-child(1)
li:nth-child(1)

1-based, per parent

.eq(0)
$("li").eq(0)

0-based, flat set

:nth-child(even)
tr:nth-child(even)

Sibling position

:even
$("tr:even")

Flat list filter

Examples Gallery

Example 1 follows the official jQuery demo. Examples 2–5 cover even/odd stripes, 3n patterns, offset equations, and the critical :nth-child() vs .eq() difference. Use Try-it links to experiment live.

📚 Official jQuery Demo

Mark the second list item in every ul.

Example 1 — Official Demo: ul li:nth-child(2)

Append - 2nd! to the second li in each matched ul. Only lists with at least two children match.

jQuery
$( "ul li:nth-child(2)" ).append( " - 2nd! " );

// Matches second li in each ul — Brandon and Ralph in the official demo
Try It Yourself

How It Works

Each ul is evaluated independently. The selector requires the matched li to be the second child of that ul — counting starts at 1, not 0.

📈 Pattern Selectors

Even/odd stripes and An+B equations from the official playground demo.

Example 2 — Zebra Stripes: tr:nth-child(even)

Style even-position rows. Unlike jQuery’s :even, this respects child index within each parent.

jQuery
$( "tr:nth-child(even)" ).css( "background", "#dbeafe" );

// Official demo also compares :nth-child(odd) with .odd() — different models
Try It Yourself

How It Works

even is shorthand for 2n in CSS. Child counting resets inside each parent — ideal for tables and repeated list blocks.

Example 3 — Every Third Item: li:nth-child(3n)

Official playground button :nth-child(3n) — matches items 3, 6, 9…

jQuery
$( "li:nth-child(3n)" ).addClass( "highlight" );

// 3n means positions where index is divisible by 3
Try It Yourself

How It Works

The equation 3n generates the sequence 3, 6, 9… Use it for periodic emphasis — every third row, card, or bullet.

Example 4 — Offset Pattern: li:nth-child(3n+1)

Official demo button :nth-child(3n+1) — matches 1, 4, 7, 10… Perfect for marking the start of repeating groups.

jQuery
$( "li:nth-child(3n+1)" ).addClass( "lead" );

// Also try :nth-child(3n+2) for middle items in each group of three
Try It Yourself

How It Works

An+B is the general CSS formula. With A=3 and B=1, you offset the cycle so the first item of each group of three is selected.

Example 5 — :nth-child(1) vs .eq(1) (Official Warning)

Official docs: given two li elements, nth-child(1) selects the first while .eq(1) selects the second — different indexing models.

jQuery
// 1-based child position among ALL siblings:
$( "#list li:nth-child(1)" ).addClass( "nth" );   // Alpha

// 0-based index in the matched jQuery set:
$( "#list li" ).eq( 1 ).addClass( "eq" );           // Beta (second li)
Try It Yourself

How It Works

:nth-child(n) counts among all children of each parent and requires the element type to match. .eq(n) slices the jQuery result set with 0-based indexing — no parent awareness.

🚀 Common Use Cases

  • Table stripestr:nth-child(even) for alternating row colors.
  • Navigation — style the second menu item with li:nth-child(2).
  • Grid layouts — clear every third card using div:nth-child(3n).
  • Grouped lists — mark group starts with li:nth-child(3n+1).
  • Repeating forms — highlight every other field row for readability.
  • Data tables — official playground pattern with clickable :nth-child buttons.

🧠 How jQuery Evaluates :nth-child()

1

Find candidates

Collect elements matching the base selector — e.g. all li nodes in scope.

Query
2

Count siblings

For each candidate, determine its 1-based index among all children of the same parent.

Index
3

Test formula

Keep the element if its index satisfies the number, even/odd, or An+B rule.

Match
4

Return collection

All passing elements — often several, one per parent — become a jQuery object.

📝 Notes

  • Available since jQuery 1.1.4 — implementation follows the CSS specification strictly.
  • Counting is 1-indexed; :nth-child(1) equals :first-child.
  • All sibling elements count toward the index — not only elements matching the base selector.
  • :nth-child(even) differs from jQuery’s :even — parent context vs flat filtering.
  • Official docs recommend understanding the .eq() distinction before using both in one script.
  • Native querySelectorAll("li:nth-child(2)") uses identical syntax in modern browsers.

Browser Support

The :nth-child() pseudo-class is standard CSS3 and works in jQuery 1.1.4+. An+B equations, even, and odd are supported in all evergreen browsers via native querySelectorAll.

CSS3 · jQuery 1.1.4+

jQuery :nth-child() Selector

Supported in all modern browsers. Native: document.querySelectorAll("tr:nth-child(even)"). jQuery follows CSS 1-based child indexing.

100% Standard + jQuery
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 · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
:nth-child() Universal

Bottom line: Safe for modern apps. Remember 1-based indexing vs .eq() 0-based indexing — official jQuery docs call out this confusion explicitly.

Conclusion

The :nth-child() selector is your tool for positional patterns inside a parent — fixed indexes, zebra stripes, and repeating cycles via An+B. The official ul li:nth-child(2) demo shows how each list is evaluated independently.

Keep the 1-based CSS counting model separate from jQuery’s 0-based .eq(), and prefer :nth-child(even) over :even when sibling position within a parent matters.

💡 Best Practices

✅ Do

  • Use tr:nth-child(even) for table zebra stripes
  • Count siblings from 1 when reading :nth-child()
  • Scope with a parent: $("#menu li:nth-child(2)")
  • Try official An+B patterns: 3n, 3n+1
  • Use .eq() when slicing an existing collection

❌ Don’t

  • Confuse :nth-child(2) with .eq(2)
  • Assume only matching tags count toward the index
  • Swap :nth-child(even) and :even blindly
  • Forget that empty lists produce zero matches
  • Expect :nth-child() to mutate the DOM

Key Takeaways

Knowledge Unlocked

Five things to remember about :nth-child()

Positional patterns with 1-based child indexing.

5
Core concepts
2n 02

even/odd

Zebra rows

Pattern
3n 03

An+B

Repeating cycles

Formula
.eq 04

Not .eq()

0-based set

Pitfall
05

Official

nth-child(2)

Demo

❓ Frequently Asked Questions

:nth-child(index) selects every element that is the nth child of its parent, counting all sibling elements from 1. $("li:nth-child(2)") matches the second li in each list — but only if that li is actually the second child overall (any tag counts).
CSS and jQuery :nth-child() use 1-based counting — the first child is :nth-child(1). This differs from jQuery methods like .eq() and .first(), which use 0-based indexing in the matched set.
:nth-child(n) checks position among all children of each parent and can match multiple elements (one per parent). .eq(n) picks a single element from the current jQuery collection using 0-based index — it ignores parent/child relationships.
:nth-child(even) and :nth-child(odd) stripe rows by child position within each parent. Equations like :nth-child(3n) match every 3rd child (3, 6, 9…); :nth-child(3n+1) matches 1, 4, 7… Official jQuery docs include a playground demo for these strings.
Yes. :first-child is equivalent to :nth-child(1) — both match elements that are the first child of their parent. Use :nth-child() when you need positions beyond the first.
:nth-child(even) counts child position within each parent — sibling index matters. jQuery's :even filters the flat list of matched elements regardless of parent, similar to .filter(":even"). They can select different nodes.
Did you know?

Official jQuery documentation includes an interactive playground where buttons apply :nth-child(even), :nth-child(3n), and :nth-child(3n+1) to table rows — and compares them with .even() and .odd(). The visual difference makes the parent-aware counting model click instantly.

Continue to :nth-last-child() Selector

After forward child indexes, learn reverse counting from the last sibling.

:nth-last-child() tutorial →

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.

6 people found this page helpful