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
Fundamentals
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.
Concept
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").
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
Hands-On
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.
City, 28, London columns (last td per row) → gray + bold
Name and Age columns → unchanged
How It Works
Each tr is a parent — the last td in every row matches, including the header row.
Applications
🚀 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.
Important
📝 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.
Compatibility
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.
Bottom line: Use li:last-child or div span:last-child — scope to parents; prefer over bare :last-child.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :last-child
Last child of each parent.
5
Core concepts
last01
:last-child
Per parent
API
n02
Many
Not .last()
Rule
nth03
nth-last(1)
Equivalent
CSS
demo04
Official
span hover
Demo
CSS05
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.