The :nth-last-of-type() selector matches elements by reverse position among same-tag siblings — not among every child. Available since jQuery 1.9; counting is 1-based from the last element of that type. Ideal when mixed markup (spans, paragraphs, headings) breaks plain :nth-last-child() rules.
01
Syntax
:nth-last-of-type(n)
02
By tag
Same type only
03
From end
1 = last of type
04
Official
li:nth-last-of-type(2)
05
vs child
Mixed tags
06
An+B
even, 3n
Fundamentals
Introduction
Real pages mix element types inside one container — a <div> with spans, paragraphs, and headings. Selectors like :nth-last-child() count every sibling, so a footer <p> can shift indexes for spans above it. :nth-last-of-type() fixes that by counting only among siblings that share the same tag name.
jQuery has supported this selector since version 1.9. Official docs describe it as selecting the nth child in relation to siblings with the same element name, counting from the last element backward — still 1-indexed, like other CSS nth- pseudo-classes.
Concept
Understanding the :nth-last-of-type() Selector
Read p:nth-last-of-type(2) as: “Among all p siblings under this parent, select the one that is second from the bottom.” A trailing h3 or span does not affect the count — only other p elements do.
span:nth-last-of-type(1) → last span, even if a p follows it.
li:nth-last-of-type(2) → penultimate li (official demo).
p:nth-last-of-type(even) → even-position paragraphs from the bottom among p only.
li:nth-last-of-type(3n) → every 3rd li from the end by type index.
💡
Beginner Tip
Always prefix with the element type — p:nth-last-of-type(2), not bare :nth-last-of-type(2). The tag name defines which sibling group gets counted.
Foundation
📝 Syntax
Official jQuery API form (since 1.9):
jQuery
jQuery( ":nth-last-of-type(index/even/odd/equation)" )
// fixed reverse index among same tag (1-based):
$( "ul li:nth-last-of-type(2)" ) // 2nd-to-last li
$( "span:nth-last-of-type(1)" ) // last span (= :last-of-type)
// keywords and equations (among same type only):
$( "p:nth-last-of-type(even)" )
$( "p:nth-last-of-type(odd)" )
$( "li:nth-last-of-type(3n)" )
$( "li:nth-last-of-type(3n+1)" )
Parameters
index — positive integer from the last same-type sibling, even/odd, or an An+B CSS equation.
Return value
A jQuery object containing every element matching the tag and reverse nth-of-type rule within its parent.
Can match multiple elements when several parents each qualify.
Official jQuery API example
jQuery
// Find the second-to-last li in each matched ul and note it
$( "ul li:nth-last-of-type(2)" ).append( " - 2nd to last! " );
Cheat Sheet
⚡ Quick Reference
Goal
Selector
Last of tag
p:nth-last-of-type(1)
Second-to-last of tag
li:nth-last-of-type(2)
Even paragraphs from bottom
p:nth-last-of-type(even)
Every 3rd li from end
li:nth-last-of-type(3n)
Counts all child types
Use :nth-last-child() instead
Compare
📋 :nth-last-of-type() vs :nth-last-child() and :last-of-type
Type-based reverse counting vs all-sibling counting — critical when tags are mixed.
Of-type
span:nth-last-of-type(1)
Last span only
Last-child
span:nth-last-child(1)
Span must be last child
:last-of-type
p:last-of-type
= nth-last-of-type(1)
Mixed tags
p after span
Of-type still works
Hands-On
Examples Gallery
Example 1 follows the official jQuery demo. Examples 2–5 cover last-of-type equivalence, the key difference from :nth-last-child(), reverse even stripes among paragraphs, and 3n tail patterns. Use Try-it links to run each snippet.
📚 Official jQuery Demo
Mark the second-to-last li of each type in every list.
Example 1 — Official Demo: ul li:nth-last-of-type(2)
Append - 2nd to last! to the penultimate li in each ul — Adam and Timmy in the official markup.
jQuery
$( "ul li:nth-last-of-type(2)" ).append( " - 2nd to last! " );
// Counts only li siblings — same result as nth-last-child(2) in pure li lists
First ul: "Adam - 2nd to last!"
Single-li ul: no match
Third ul: "Timmy - 2nd to last!"
How It Works
Among li siblings only, reverse index 2 targets the penultimate list item in each ul. When a list contains only li children, this often matches :nth-last-child(2) too.
📈 Type-Based Reverse Patterns
Last-of-type shorthand, mixed markup, stripes, and equations.
Example 2 — Last of Type: span:nth-last-of-type(1)
Official docs: with three spans, nth-last-of-type(1) selects the last span — equivalent to :last-of-type for that tag.
"Gamma" highlighted — last span
Footer p does not prevent the match
How It Works
Even though a p is the last child, Gamma is still the last span among span siblings — so it matches.
Example 3 — Mixed Tags: nth-last-of-type(1) vs nth-last-child(1)
When a different tag is the last child, only the of-type selector matches the last span.
jQuery
$( "span:nth-last-of-type(1)" ).addClass( "of-type" ); // Span B
$( "span:nth-last-child(1)" ).addClass( "of-child" ); // no match
// p is last child — span fails nth-last-child(1)
Items at reverse positions 3, 6, 9 among li siblings highlighted
Pure li lists behave like nth-last-child(3n)
How It Works
The same An+B grammar as forward :nth-of-type(), applied to reverse indexes within one tag group.
Applications
🚀 Common Use Cases
Last paragraph — p:nth-last-of-type(1) when a footer div follows.
Penultimate list item — li:nth-last-of-type(2) in mixed-content lists.
Article stripes — p:nth-last-of-type(even) with headings between paragraphs.
Table columns by tag — reverse patterns on repeated td groups.
Card grids — div:nth-last-of-type(3n) when each card shares a wrapper tag.
Official playground — click :nth-last-of-type buttons to preview table rows.
🧠 How jQuery Evaluates :nth-last-of-type()
1
Find candidates
Gather elements matching the tag selector — e.g. all p or li in scope.
Query
2
Group by type
Within each parent, consider only siblings sharing the same element name.
Filter
3
Count from end
Apply 1-based reverse index or An+B formula among that type group.
Match
4
📦
Return collection
Qualifying elements become a jQuery object — often one match per parent.
Important
📝 Notes
Available since jQuery 1.9 — follows the W3C CSS specification strictly.
Counting is 1-indexed from the last same-type sibling.
:last-of-type equals :nth-last-of-type(1) for a given tag.
Only siblings with the same tag name participate — unlike :nth-last-child().
Official docs use the same even, odd, and An+B syntax as other nth- selectors.
Native querySelectorAll("p:nth-last-of-type(2)") works in modern browsers.
Compatibility
Browser Support
The :nth-last-of-type() pseudo-class is standard CSS3 and works in jQuery 1.9+. An+B equations and even/odd are supported in evergreen browsers via native querySelectorAll.
✓ CSS3 · jQuery 1.9+
jQuery :nth-last-of-type() Selector
Supported in all modern browsers. Native: document.querySelectorAll("p:nth-last-of-type(2)"). Reverse 1-based indexing among same-tag siblings.
100%Standard + jQuery
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
:nth-last-of-type()Universal
Bottom line: Safe for modern apps. Choose of-type over last-child when sibling tags differ — the main practical distinction.
Wrap Up
Conclusion
The :nth-last-of-type() selector combines reverse indexing with type-scoped sibling counting — the tool you need when footers, headings, or mixed blocks sit beside repeated tags. The official ul li:nth-last-of-type(2) demo shows the penultimate-item pattern in action.
Remember :nth-last-of-type(1) = :last-of-type, and reach for this selector instead of :nth-last-child() whenever non-matching tags appear among siblings.
Use when mixed sibling tags break last-child rules
Scope: article p:nth-last-of-type(even)
Pair with :first-of-type / :last-of-type tutorials
Try official playground An+B buttons
❌ Don’t
Confuse with :nth-last-child() on mixed markup
Use bare $(":nth-last-of-type(2)") without a tag
Assume class names affect of-type counting — only tags matter
Expect matches when fewer same-type siblings exist
Swap in .eq(-2) — different indexing model
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :nth-last-of-type()
Reverse index among same-tag siblings only.
5
Core concepts
📝01
By tag
Same type only
Scope
102
Reverse
1 = last of type
Index
≠03
≠ last-child
Mixed tags
Rule
=04
:last-of-type
nth-last(1)
Equiv
✓05
Official
nth-last-of-type(2)
Demo
❓ Frequently Asked Questions
:nth-last-of-type(index) selects every element that is the nth sibling of its tag name when counting backward from the last same-type sibling. $("li:nth-last-of-type(2)") matches the second-to-last li in each parent — counting only li elements, not other tags.
:nth-last-child() counts among all sibling element types. :nth-last-of-type() counts only among siblings with the same tag name. A span before a trailing p can be span:nth-last-of-type(1) but not span:nth-last-child(1).
Yes. :last-of-type is equivalent to :nth-last-of-type(1) for a given element type. Both select the last element of that tag name among siblings under the same parent.
Yes. Like other CSS nth- selectors in jQuery, counting starts at 1 from the last same-type sibling. Official docs: given three li elements, li:nth-last-of-type(1) selects the third (last) li.
Official jQuery docs support :nth-last-of-type(even), :nth-last-of-type(odd), :nth-last-of-type(3n), and :nth-last-of-type(3n+1) — evaluated among same-tag siblings from the end, independent of other element types in the parent.
jQuery 1.9 added :nth-last-of-type() alongside :first-of-type, :last-of-type, and 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-of-type(even), :nth-last-of-type(3n), and :nth-last-of-type(3n+1) to table rows — the same interactive pattern as the forward nth- selector demos, but counting from the end among same-type siblings.