The :last-of-type selector matches every element that is the last of its tag name among siblings. It can return multiple nodes — one per parent. Standard CSS since jQuery 1.9; equivalent to :nth-last-of-type(1).
01
Syntax
span:last-of-type
02
Per parent
Many matches
03
Official
span + solast
04
vs :last-child
Tag-based
05
nth-last-of-type
Equivalent
06
CSS
Standard
Fundamentals
Introduction
Real markup mixes element types — spans in a name list, then a trailing paragraph or footer note. You often need the last <span> or last <p> in each block, even when another tag is the overall last child. The CSS :last-of-type pseudo-class selects by element name among siblings, not absolute child position.
jQuery supports :last-of-type in selector strings since version 1.9. Official documentation states that it matches elements with no other element of the same name and parent coming after them in the document tree — so $("span:last-of-type") can match many spans, one per parent.
Concept
Understanding the :last-of-type Selector
Think of :last-of-type as “the last sibling of this tag name under each parent”:
<div><span>A</span><span>B</span></div> → last span matches span:last-of-type.
Second <div> with its own last span → also matches.
<div><span>…</span><p>…</p></div> → span matches span:last-of-type but not span:last-child.
Equivalent to :nth-last-of-type(1) in standard CSS.
💡
Beginner Tip
When siblings are all the same tag (e.g. only <li> in a list), :last-of-type and :last-child behave the same. They diverge when mixed element types share a parent.
📋 :last-of-type vs :last-child, .last(), and :nth-last-of-type(1)
Tag-based sibling position vs absolute last child vs jQuery set index vs typed nth-from-end index.
:last-of-type
span:last-of-type
Last span per parent
:last-child
span:last-child
Must be last child
.last()
$("span").last()
Last in matched set
:nth-last-of-type(1)
p:nth-last-of-type(1)
Equivalent
Hands-On
Examples Gallery
Example 1 follows the official jQuery span:last-of-type demo. Examples 2–5 contrast with :last-child, style article closing paragraphs, verify :nth-last-of-type(1), and highlight the last table column.
📚 Official jQuery Demo
Style the last span in each matched div — red, 80% size; hover adds solast (line-through).
Example 1 — Official Demo: span:last-of-type
Official jQuery demo — Todd and Timo styled red at 80% font size; hover adds solast (line-through). Other spans unchanged.
City 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. With only td children, :last-of-type and :last-child select the same cells.
Applications
🚀 Common Use Cases
Closing paragraphs — article p:last-of-type for summary or sign-off text.
Inline name lists — official pattern with span:last-of-type.
Section footers — section p:last-of-type when markup mixes tags.
Tables — highlight td:last-of-type action or total columns.
Definition lists — style the last dt or dd in each group.
CSS parity — same selector works in stylesheets and jQuery.
🧠 How jQuery Evaluates :last-of-type
1
Parse selector
Combine type selector with :last-of-type — e.g. span:last-of-type.
Query
2
Check siblings
For each candidate, verify no same-tag sibling appears later under the same parent.
CSS
3
Collect matches
One match per parent per tag — can be many elements document-wide.
Set
4
✓
Return collection
Chain .addClass(), .css(), or other jQuery methods.
Important
📝 Notes
Available since jQuery 1.9 — standard CSS pseudo-class.
Equivalent to :nth-last-of-type(1) — standard CSS specification.
Can match multiple elements — one last-of-type per parent for the chosen tag.
Differs from :last-child when siblings of other element types follow the target.
Works in native querySelectorAll — no jQuery extension caveat.
Always pair with an element type — p:last-of-type, not bare :last-of-type on large pages.
Compatibility
Browser Support
The :last-of-type pseudo-class is standard CSS and works in jQuery 1.9+. All modern browsers support document.querySelectorAll("p:last-of-type"). Same syntax in stylesheets and jQuery selectors.
Bottom line: Use p:last-of-type or article p:last-of-type — scope to parents; prefer over bare :last-of-type.
Wrap Up
Conclusion
The :last-of-type selector matches every element that is the last of its tag name among siblings — the official span:last-of-type demo styles Todd and Timo red at 80% with hover solast line-through, not the other spans.
Distinguish it from :last-child and .last(), remember equivalence with :nth-last-of-type(1), and scope your selector to the parents you care about.
Use article p:last-of-type for scoped closing paragraphs
Choose :last-of-type when mixed sibling tags break :last-child
Remember :nth-last-of-type(1) is equivalent
Reuse the same selector in CSS and jQuery
Compare with :last-child when debugging unexpected matches
❌ Don’t
Assume :last-of-type equals :last-child without checking markup
Confuse :last-of-type with .last() — different models
Use bare $(":last-of-type") on large pages
Confuse :nth-last-of-type with :nth-last-child — different counting rules
Forget to scope — prefix with a parent selector when possible
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :last-of-type
Last of each tag name per parent.
5
Core concepts
last01
:last-of-type
Per tag per parent
API
≠02
Not :last-child
Mixed tags differ
Rule
nth03
nth-last-of-type(1)
Equivalent
CSS
demo04
Official
span + solast
Demo
CSS05
Standard
Native CSS
Tip
❓ Frequently Asked Questions
:last-of-type selects every element that is the last among siblings with the same element name (tag). $("span:last-of-type") matches the last span in each parent — Todd and Timo in the official demo. Available since jQuery 1.9.
:last-child requires the element to be the last child of its parent among all element types. :last-of-type only compares siblings with the same tag. If a <p> comes after a <span>, the span is not :last-child but can still be span:last-of-type.
Yes. In CSS, :last-of-type is equivalent to :nth-last-of-type(1). Both select the last element of that tag name among siblings under the same parent.
Yes. :last-of-type is standard CSS and works in native querySelectorAll — e.g. document.querySelectorAll("p:last-of-type"). jQuery has supported it since 1.9.
Another element type may be the last child. Example: <div><span>A</span><span>B</span><p>Footer</p></div> — the last span matches span:last-of-type but not span:last-child because the paragraph is the last child.
Yes — $("p:last-of-type") or $("article p:last-of-type") is clearer than bare $(":last-of-type"). Scoping to a parent class or ID avoids matching unintended last-of-type nodes across the page.
Did you know?
When mixed sibling tags follow your target, :last-of-type still matches but :last-child does not. A <div> with spans then a trailing <p> — the last span matches span:last-of-type but not span:last-child because the paragraph is the last child.