jQuery :last-child Selector

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

What You’ll Learn

The :last-child selector matches every element that is the last child of its parent. Unlike .last(), it can return multiple nodes — one per parent. Standard CSS since jQuery 1.1.4; equivalent to :nth-last-child(1).

01

Syntax

span:last-child

02

Per parent

Many matches

03

Official

div span

04

vs .last()

One global

05

nth-last(1)

Equivalent

06

CSS

Standard

Introduction

Layout patterns often treat the last item in a group differently — remove trailing margin on the final nav link, emphasize the closing name in a list, or align the rightmost column in a table. The CSS :last-child pseudo-class selects those elements based on sibling position within each parent.

jQuery supports :last-child in selector strings since version 1.1.4. Official documentation emphasizes that while .last() matches only a single element, :last-child can match more than one — one for each parent in the document.

Understanding the :last-child Selector

Think of :last-child as “the final sibling under each parent”:

  • <div><span>A</span><span>B</span></div> → last span matches span:last-child.
  • Second <div> with its own last span → also matches.
  • <div><span>…</span><p>…</p></div> → span is not last child — no match for span:last-child.
  • Equivalent to :nth-last-child(1) per official jQuery docs.
💡
Beginner Tip

Need only the very last element in a flat list? Use $("li").last(). Need the last item in every list? Use $("ul li:last-child").

📝 Syntax

Official jQuery API form (since 1.1.4):

jQuery
jQuery( ":last-child" )

// typical usage — prefix with element:
$( "div span:last-child" )
$( "ul.nav li:last-child" )
$( "tr td:last-child" )

// equivalent:
$( "p:nth-last-child(1)" )

Parameters

  • No arguments — matches elements that are the last child of their parent.

Return value

  • A jQuery object containing every matching last-child element (often multiple).
  • Empty collection when no element is a last child in scope.

Official jQuery API example

jQuery
$( "div span:last-child" )
  .css({ color: "red", fontSize: "80%" })
  .hover( function() {
    $( this ).addClass( "solast" );
  }, function() {
    $( this ).removeClass( "solast" );
  });

⚡ Quick Reference

Goal:last-child.last()
Last span in each divdiv span:last-childNot the same
Last li in document orderMany (one per ul)$("li").last()
Same as nth-last-child:nth-last-child(1)N/A
Last table columntr td:last-childNot the same
Native CSSSupportedjQuery method only

📋 :last-child vs .last() and :eq(-1)

Sibling position per parent vs global index in a matched set.

:last-child
li:last-child

Last per parent

.last()
$("li").last()

One element total

:nth-last-child(1)
p:nth-last-child(1)

Equivalent

:eq(-1)
li:eq(-1)

Last in set index

Examples Gallery

Example 1 follows the official jQuery div span:last-child demo. Examples 2–5 contrast with .last(), style navigation lists, verify :nth-last-child(1), and highlight table columns.

📚 Official jQuery Demo

Style the last span in each div with red 80% text and a hover strikethrough.

Example 1 — Official Demo: div span:last-child

Official jQuery demo — Sam and David styled red at 80% size; hover adds strikethrough via solast class.

jQuery
$( "div span:last-child" )
  .css({ color: "red", fontSize: "80%" })
  .hover( function() {
    $( this ).addClass( "solast" );
  }, function() {
    $( this ).removeClass( "solast" );
  });
Try It Yourself

How It Works

Each div has its own last span child — Sam in the first div and David in the second, two matches total from the official markup.

Example 2 — :last-child vs .last()

Show why :last-child returns one item per list while .last() returns a single node.

jQuery
$( "li:last-child" ).addClass( "lc" );
$( "li" ).last().addClass( "one" );

console.log( ":last-child →", $( "li:last-child" ).length );
console.log( ".last() →", $( "li" ).last().length );
Try It Yourself

How It Works

Official docs: .last() matches only a single element; :last-child can match more than one.

📈 Practical Patterns

Navigation, equivalence, and table layouts.

Example 3 — Nav Lists: ul.nav li:last-child

Emphasize the last link in every navigation list.

jQuery
$( "ul.nav li:last-child" ).css({
  fontWeight: "bold",
  color: "#2563eb"
});
Try It Yourself

How It Works

Scoped ul.nav limits which lists are affected — each list contributes one last li.

Example 4 — Equivalence: :nth-last-child(1)

Official docs — :last-child is equivalent to :nth-last-child(1).

jQuery
console.log( ":last-child →", $( "p:last-child" ).length );
console.log( ":nth-last-child(1) →", $( "p:nth-last-child(1)" ).length );
Try It Yourself

How It Works

Both use CSS child indexing from the end — the element must be the last child of its parent among all element siblings.

Example 5 — Table Column: tr td:last-child

Style the last cell in every row — useful for action columns or right-aligned values.

jQuery
$( "tr td:last-child" ).css({
  backgroundColor: "#f1f5f9",
  fontWeight: "bold"
});
Try It Yourself

How It Works

Each tr is a parent — the last td in every row matches, including the header row.

🚀 Common Use Cases

  • Navigation — style li:last-child as the contact or settings link.
  • Inline name lists — official demo pattern with span:last-child.
  • Tables — highlight td:last-child action or value columns.
  • Card grids — remove right margin on .card:last-child per row wrapper.
  • Form rows — style submit buttons that are the last child in a field group.
  • CSS parity — same selector works in stylesheets and jQuery.

🧠 How jQuery Evaluates :last-child

1

Parse selector

Combine type selector with :last-child — e.g. div span:last-child.

Query
2

Check parent

For each candidate, verify it is the last element child of its parent.

CSS
3

Collect matches

One match per parent — can be many elements document-wide.

Set
4

Return collection

Chain .css(), .hover(), or other jQuery methods.

📝 Notes

  • Available since jQuery 1.1.4 — standard CSS pseudo-class.
  • Equivalent to :nth-last-child(1) — official jQuery documentation.
  • Can match multiple elements — unlike .last() which returns one.
  • Element must be the last child of its parent — trailing siblings block a match.
  • Works in native querySelectorAll — no jQuery extension caveat.
  • Not the same as :last-of-type — that ignores siblings of other tag names.

Browser Support

The :last-child pseudo-class is standard CSS and works in jQuery 1.1.4+. All modern browsers support document.querySelectorAll("li:last-child"). Same syntax in stylesheets and jQuery selectors.

CSS · jQuery 1.1.4+

jQuery :last-child Selector

Universal browser support. Native: querySelectorAll("span:last-child").

100% Standard CSS
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
:last-child Universal

Bottom line: Use li:last-child or div span:last-child — scope to parents; prefer over bare :last-child.

Conclusion

The :last-child selector matches every element that is the last child of its parent — the official div span:last-child demo styles Sam and David red at 80%, not the other spans.

Distinguish it from .last() and :eq(-1), remember equivalence with :nth-last-child(1), and scope your selector to the parents you care about.

💡 Best Practices

✅ Do

  • Use ul.nav li:last-child for scoped queries
  • Choose :last-child when every parent needs a last item styled
  • Use .last() when you need one global match
  • Remember :nth-last-child(1) is equivalent
  • Reuse the same selector in CSS and jQuery

❌ Don’t

  • Confuse :last-child with :last-of-type without reading docs
  • Assume the last element in document order — check parent context
  • Use bare $(":last-child") on large pages
  • Expect a match when a different element is the last child
  • Replace :last-child with :eq(-1) — different indexing models

Key Takeaways

Knowledge Unlocked

Five things to remember about :last-child

Last child of each parent.

5
Core concepts
n 02

Many

Not .last()

Rule
nth 03

nth-last(1)

Equivalent

CSS
demo 04

Official

span hover

Demo
CSS 05

Standard

Native CSS

Tip

❓ Frequently Asked Questions

:last-child selects every element that is the last child of its parent. $("div span:last-child") matches the last span inside each div — so multiple elements can match. Available since jQuery 1.1.4.
.last() returns only one element — the last in the entire jQuery matched set. :last-child can return many — one per parent. Official jQuery docs highlight this distinction explicitly.
Yes. Official jQuery documentation states that :last-child is equivalent to :nth-last-child(1). Both select elements that are the last child of their parent (1-based child index counted from the end in CSS).
Yes. :last-child is standard CSS and works in native querySelectorAll — e.g. document.querySelectorAll("li:last-child"). jQuery has supported it since 1.1.4.
If another element sibling comes after your target, it will not match. :last-child checks sibling position among all element children of the parent — only the final element child matches, regardless of tag name.
Yes — $("li:last-child") or $("div span:last-child") is clearer than bare $(":last-child"). Scoping to a parent class or ID avoids matching unintended last children across the page.
Did you know?

:last-child and :last-of-type differ when siblings of other element types come after. If a <span> precedes a <p>, the span is not :last-child but may still be :last-of-type among spans.

Continue to :only-child Selector

Learn how to target elements with no element siblings — the official lone-button demo.

:only-child selector 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