The :nth-last-child() selector is the mirror of :nth-child(): it matches elements by position counting backward from the last sibling. Available since jQuery 1.9; indexing is 1-based from the end. Pick the penultimate row, style the last child, or apply even/odd patterns from the bottom up.
01
Syntax
:nth-last-child(n)
02
From end
1 = last child
03
2nd last
:nth-last-child(2)
04
Official
Append label demo
05
An+B
3n, even
06
vs forward
:nth-child()
Fundamentals
Introduction
Sometimes you care about items near the bottom of a list — the second-to-last table row, the final nav link, or every third item counting backward. CSS provides :nth-last-child() for exactly that, and jQuery has supported it since version 1.9.
The counting model matches :nth-child() but reverses direction: 1 means the last child, 2 means second-to-last, and so on. Official docs note that given three li elements, li:nth-last-child(1) selects the third (last) item — still 1-indexed, just measured from the tail.
Concept
Understanding the :nth-last-child() Selector
Read li:nth-last-child(2) as: “Select every li that is the second child counting backward from the end of its parent.” All sibling element types count toward the index — not only li nodes.
li:nth-last-child(1) → same as :last-child when the li is the last child.
tr:nth-last-child(even) → even positions from the bottom within each parent.
li:nth-last-child(3n) → every 3rd item from the end (official playground).
💡
Beginner Tip
When markup grows at the top (new rows prepended), selectors counting from the end stay stable. Prefer :nth-last-child() over forward indexes when the tail position matters more than the head.
Foundation
📝 Syntax
Official jQuery API form (since 1.9):
jQuery
jQuery( ":nth-last-child(index/even/odd/equation)" )
// fixed index from the end (1-based):
$( "ul li:nth-last-child(2)" ) // second-to-last
$( "li:nth-last-child(1)" ) // last child
// keywords and equations:
$( "tr:nth-last-child(even)" )
$( "tr:nth-last-child(odd)" )
$( "li:nth-last-child(3n)" )
$( "li:nth-last-child(3n+1)" )
$( "li:nth-last-child(3n+2)" )
Parameters
index — positive integer from the last child (1 = last), even/odd, or an An+B CSS equation.
Return value
A jQuery object with every element matching the base selector and the reverse nth-child rule within its parent.
Multiple matches when several parents each contain a qualifying child.
Official jQuery API example
jQuery
// Find the second-to-last li in each matched ul and note it
$( "ul li:nth-last-child(2)" ).append( " - 2nd to last! " );
Cheat Sheet
⚡ Quick Reference
Goal
Selector
Last child
li:nth-last-child(1)
Second-to-last
li:nth-last-child(2)
Even from bottom
tr:nth-last-child(even)
Every 3rd from end
li:nth-last-child(3n)
Forward equivalent
:nth-child() counts from top
Compare
📋 :nth-last-child() vs :nth-child() and :last-child
Same 1-based index number — opposite direction within each parent.
From end
li:nth-last-child(2)
2nd from bottom
From start
li:nth-child(2)
2nd from top
Last child
:nth-last-child(1)
= :last-child
.eq(-1)
$("li").eq(-1)
Flat set, not per parent
Hands-On
Examples Gallery
Example 1 follows the official jQuery demo. Examples 2–5 cover last-child equivalence, reverse even stripes, 3n from the tail, and a side-by-side comparison with :nth-child(). Open Try-it labs to experiment live.
📚 Official jQuery Demo
Mark the second-to-last list item in every ul.
Example 1 — Official Demo: ul li:nth-last-child(2)
Append - 2nd to last! to the penultimate li in each list — Adam and Timmy in the official markup.
jQuery
$( "ul li:nth-last-child(2)" ).append( " - 2nd to last! " );
// Evaluated per ul — lists with fewer than 2 items do not match
Items 3, 6, 9 from the end highlighted
Different set than li:nth-child(3n)
How It Works
The same An+B grammar as forward :nth-child(), applied to reverse sibling indices. Official demo buttons let you click and compare patterns instantly.
Example 5 — Forward vs Reverse: nth-child(2) vs nth-last-child(2)
Same index, different direction — in a five-item list, forward picks B while reverse picks D.
jQuery
$( "#list li:nth-child(2)" ).addClass( "from-start" ); // B
$( "#list li:nth-last-child(2)" ).addClass( "from-end" ); // D
// .eq(-2) selects from a flat jQuery set — yet another model
nth-child(2) → B (blue outline)
nth-last-child(2) → D (orange outline)
How It Works
Both use 1-based CSS counting within a parent — only the direction changes. Pick forward when the head matters; pick reverse when the tail matters.
Applications
🚀 Common Use Cases
Penultimate row — tr:nth-last-child(2) before a footer row.
Last item styling — li:nth-last-child(1) without a separate :last-child rule.
Bottom-anchored stripes — tr:nth-last-child(even) when rows prepend at the top.
Repeating tail groups — li:nth-last-child(3n+1) for cycles from the end.
Nav menus — highlight second-to-last link with li:nth-last-child(2).
Official playground — click :nth-last-child buttons to preview table patterns.
🧠 How jQuery Evaluates :nth-last-child()
1
Find candidates
Gather elements matching the base selector — e.g. all li nodes in scope.
Query
2
Count from end
For each candidate, compute its 1-based index among all siblings, measuring backward from the last child.
Reverse
3
Test formula
Keep the element if its reverse index satisfies the number, even/odd, or An+B rule.
Match
4
📦
Return collection
Qualifying elements — often one per parent — become a jQuery object for chaining.
Important
📝 Notes
Available since jQuery 1.9 — follows the W3C CSS specification strictly.
Counting is 1-indexed from the last child; :nth-last-child(1) equals :last-child.
All sibling element types count — not only elements matching the base selector tag.
Official docs distinguish this from .eq(), which uses 0-based indexing on a flat matched set.
:nth-last-child(even) and :nth-child(even) can highlight different rows in the same table.
Native querySelectorAll("li:nth-last-child(2)") uses identical syntax in modern browsers.
Compatibility
Browser Support
The :nth-last-child() pseudo-class is standard CSS3 and works in jQuery 1.9+. An+B equations and even/odd keywords are supported in evergreen browsers via native querySelectorAll.
✓ CSS3 · jQuery 1.9+
jQuery :nth-last-child() Selector
Supported in all modern browsers. Native: document.querySelectorAll("li:nth-last-child(2)"). Reverse 1-based child indexing per parent.
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-last-child()Universal
Bottom line: Safe for modern apps. Pair with :nth-child() tutorials — forward vs reverse is the main mental model shift.
Wrap Up
Conclusion
The :nth-last-child() selector targets positions measured from the bottom of each parent — penultimate items, last children, and reverse stripe patterns. The official ul li:nth-last-child(2) demo shows how each list is evaluated on its own.
Remember 1-based reverse counting, the equivalence :nth-last-child(1) = :last-child, and the difference from forward :nth-child() when the same index number would otherwise pick the wrong row.
Prefer reverse indexes when items prepend at the top
Scope with a parent: $("#table tr:nth-last-child(even)")
Reuse the same selector in CSS and jQuery
Compare with forward :nth-child() in Try-it labs
❌ Don’t
Assume nth-last-child(2) equals nth-child(2)
Use 0-based indexes — CSS counting starts at 1
Replace :nth-last-child(1) with .eq(-1) blindly
Forget that non-matching tags shift the child index
Confuse with :nth-last-of-type() — type-based counting
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :nth-last-child()
Count siblings backward from the last child.
5
Core concepts
📝01
Reverse
1 = last child
Index
202
2nd last
Official demo
Use
=03
:last-child
nth-last(1)
Rule
3n04
An+B
From tail
Pattern
↔05
vs forward
:nth-child()
Compare
❓ Frequently Asked Questions
:nth-last-child(index) selects every element that is the nth child of its parent when counting backward from the last sibling. $("li:nth-last-child(2)") matches the second-to-last li in each list — but only if that li occupies that position among all children.
Yes. Like :nth-child(), counting is 1-indexed per the CSS spec. :nth-last-child(1) is the last child; :nth-last-child(2) is the penultimate child. jQuery methods like .eq(-1) use different 0-based rules on a flat collection.
Yes. When the matched element is the last child of its parent, :nth-last-child(1) and :last-child select the same node. Use :nth-last-child() when you need positions further from the end.
:nth-child(n) counts from the first sibling forward; :nth-last-child(n) counts from the last sibling backward. $("li:nth-child(2)") picks the second item from the top; $("li:nth-last-child(2)") picks the second item from the bottom.
Official jQuery docs support :nth-last-child(even), :nth-last-child(odd), :nth-last-child(3n), and :nth-last-child(3n+1) — the same formula syntax as :nth-child(), but evaluated from the end of the sibling list within each parent.
jQuery 1.9 added :nth-last-child() alongside other structural pseudo-classes. It follows the W3C CSS specification and works in native querySelectorAll in modern browsers.
Did you know?
Official jQuery documentation includes a playground where buttons apply :nth-last-child(even), :nth-last-child(3n), and :nth-last-child(3n+1) to table rows — making the reverse counting model easier to see than to memorize.