jQuery :nth-last-child() Selector

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

What You’ll Learn

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()

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.

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.
  • li:nth-last-child(2) → penultimate child (official demo).
  • 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.

📝 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! " );

⚡ Quick Reference

GoalSelector
Last childli:nth-last-child(1)
Second-to-lastli:nth-last-child(2)
Even from bottomtr:nth-last-child(even)
Every 3rd from endli:nth-last-child(3n)
Forward equivalent:nth-child() counts from top

📋 :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

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
Try It Yourself

How It Works

Index 2 counts backward from the last sibling. The matched li must occupy that reverse position among all children of its ul.

📈 Reverse Patterns

Last-child shorthand, stripes, equations, and direction comparison.

Example 2 — Last Child: li:nth-last-child(1)

Official docs: with three list items, nth-last-child(1) selects the third (last) li. Equivalent to :last-child when the element is the final child.

jQuery
$( "li:nth-last-child(1)" ).addClass( "last" );

// Same idea as:
// $( "li:last-child" ).addClass( "last" );
Try It Yourself

How It Works

Reverse index 1 always targets the last child. Use the long form when you combine it with other nth expressions in one stylesheet or script.

Example 3 — Stripes from the Bottom: tr:nth-last-child(even)

Official playground button :nth-last-child(even) — even positions measured from the last row upward.

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

// Differs from :nth-child(even) when row order matters from the tail
Try It Yourself

How It Works

even still means multiples of 2, but the sequence runs from the last child backward. Useful when footer rows must stay visually fixed.

Example 4 — Every Third from the End: li:nth-last-child(3n)

Official playground pattern — selects items at reverse positions 3, 6, 9…

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

// Also try :nth-last-child(3n+1) and :nth-last-child(3n+2) in the playground
Try It Yourself

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
Try It Yourself

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.

🚀 Common Use Cases

  • Penultimate rowtr:nth-last-child(2) before a footer row.
  • Last item stylingli:nth-last-child(1) without a separate :last-child rule.
  • Bottom-anchored stripestr:nth-last-child(even) when rows prepend at the top.
  • Repeating tail groupsli: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.

📝 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.

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 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-last-child() Universal

Bottom line: Safe for modern apps. Pair with :nth-child() tutorials — forward vs reverse is the main mental model shift.

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.

💡 Best Practices

✅ Do

  • Use :nth-last-child(2) for penultimate rows
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about :nth-last-child()

Count siblings backward from the last child.

5
Core concepts
2 02

2nd last

Official demo

Use
= 03

:last-child

nth-last(1)

Rule
3n 04

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.

Continue to :first-of-type Selector

After reverse child indexes, learn how :first-of-type counts by element name among siblings.

:first-of-type 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