jQuery :last-of-type Selector

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

What You’ll Learn

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

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.

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.

📝 Syntax

Official jQuery API form (since 1.9):

jQuery
jQuery( ":last-of-type" )

// typical usage — prefix with element:
$( "span:last-of-type" )
$( "article p:last-of-type" )
$( "tr td:last-of-type" )

// equivalent:
$( "h2:nth-last-of-type(1)" )

Parameters

  • No arguments — matches elements that are the last of their element type among siblings.

Return value

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

Official jQuery API example

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

⚡ Quick Reference

Goal:last-of-type:last-child
Last span in each divspan:last-of-typeSame if span is last child
Span before a trailing <p>MatchesNo match
Last paragraph in articlearticle p:last-of-typeOnly if p is last child
Equivalent pseudo-class:nth-last-of-type(1):nth-last-child(1)
Native CSSSupportedSupported

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

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.

jQuery
$( "span:last-of-type" ).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 — Todd in div1 and Timo in div2 — two matches total from the official markup, regardless of other sibling types.

Example 2 — :last-of-type vs :last-child

When a paragraph follows spans, only :last-of-type matches the trailing span.

jQuery
$( "span:last-of-type" ).addClass( "lot" );
$( "span:last-child" ).addClass( "lc" );

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

How It Works

:last-child counts all element siblings; :last-of-type only compares elements with the same tag name.

📈 Practical Patterns

Articles, equivalence, and table layouts.

Example 3 — Article Closing: article p:last-of-type

Style the closing paragraph in every article, even when a footer or aside follows.

jQuery
$( "article p:last-of-type" ).css({
  fontStyle: "italic",
  color: "#64748b"
});
Try It Yourself

How It Works

A footer aside may be the last child, but the last p still matches p:last-of-type.

Example 4 — Equivalence: :nth-last-of-type(1)

Standard CSS — :last-of-type is equivalent to :nth-last-of-type(1).

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

How It Works

Both use CSS typed sibling indexing from the end — only elements of the same tag name are counted.

Example 5 — Table Column: tr td:last-of-type

Style the last cell in every row — same result as :last-child when rows contain only td cells.

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

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.

🚀 Common Use Cases

  • Closing paragraphsarticle p:last-of-type for summary or sign-off text.
  • Inline name lists — official pattern with span:last-of-type.
  • Section footerssection 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.

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

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.

CSS · jQuery 1.9+

jQuery :last-of-type Selector

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

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-of-type Universal

Bottom line: Use p:last-of-type or article p:last-of-type — scope to parents; prefer over bare :last-of-type.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about :last-of-type

Last of each tag name per parent.

5
Core concepts
02

Not :last-child

Mixed tags differ

Rule
nth 03

nth-last-of-type(1)

Equivalent

CSS
demo 04

Official

span + solast

Demo
CSS 05

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.

Continue to :nth-last-of-type() Selector

Learn penultimate and reverse An+B patterns among same-tag siblings — the general form of :last-of-type.

:nth-last-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