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
Fundamentals
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.
Concept
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.
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! " );
Cheat Sheet
⚡ Quick Reference
Goal
Selector
First child
li:nth-child(1) (= :first-child)
Second child
li:nth-child(2)
Even rows
tr:nth-child(even)
Every 3rd item
li:nth-child(3n)
1st of each group of 3
li:nth-child(3n+1)
Compare
📋 :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
Hands-On
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
nth-child(1) → Alpha (blue outline)
.eq(1) → Beta (orange outline)
Same number, different elements!
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.
Applications
🚀 Common Use Cases
Table stripes — tr: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.
Important
📝 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll 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.
Wrap Up
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.
: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.