The :nth-last-of-type() pseudo-class targets elements by their position among siblings of the same tag type, counting from the end. It combines reverse counting with type-specific matching.
01
By type
Same tag only.
02
Reverse count
1 = last of type.
03
odd / even
From the end.
04
-n+3
Last N of type.
05
vs nth-last-child
Mixed tags.
06
= :last-of-type
Position 1.
Fundamentals
Introduction
The :nth-last-of-type() selector in CSS lets you target elements based on their position among siblings of the same element type, counting from the last element of that type backward.
It is especially useful when a parent contains mixed tags — such as paragraphs, headings, and divs — and you need to style the last few paragraphs without being thrown off by other elements that follow.
Definition and Usage
Write element:nth-last-of-type(n) where n is a number, the keyword odd or even, or a formula like 2n+1 or -n+3.
💡
Beginner Tip
Counting starts at 1 from the last element of that type. p:nth-last-of-type(1) is the last paragraph among siblings — even if a <div> comes after it. That is the key difference from :nth-last-child().
Foundation
📝 Syntax
The syntax for the :nth-last-of-type() pseudo-class is:
syntax.css
element:nth-last-of-type(n){/* CSS properties */}
element is the tag type you want to match. n can be a number, keyword, or an+b formula.
Common Patterns
Expression
Matches (3 paragraphs + footer div)
p:nth-last-of-type(1)
Third paragraph (last p)
p:nth-last-of-type(2)
Second paragraph
p:nth-last-of-type(odd)
Third and first paragraphs
p:nth-last-of-type(even)
Second paragraph only
li:nth-last-of-type(2n)
Every 2nd li from the end
p:nth-last-of-type(-n+2)
Last two paragraphs
The an+b Formula (By Type, From the End)
n is a counter starting at 0, increasing by 1 for each sibling of the same type from the end.
a is the step size — positive steps skip toward earlier siblings of that type; negative steps select the last N of that type.
b is the offset — the starting position when counting from the last element of that type.
When n=0, the formula gives 2. When n=1, it gives 1. The negative step stops after two matches among <p> elements only.
Tips
💬 Usage Tips
Prefer :last-of-type for one item — :nth-last-of-type(1) works, but :last-of-type is more readable.
Use -n+k for last N of a type — p:nth-last-of-type(-n+3) for the last three paragraphs.
Include the element type — Always write p:nth-last-of-type(), not bare :nth-last-of-type().
Mixed siblings — This is the go-to selector when other tag types sit between your targets.
Combine with parents — article > p:nth-last-of-type(1) scopes to direct children.
Watch Out
⚠️ Common Pitfalls
Only counts same tag type — p:nth-last-of-type(1) ignores <div> siblings but only matches <p> elements.
Confusing with nth-last-child — A footer div after paragraphs breaks :nth-last-child() but not :nth-last-of-type().
1-based, not 0-based — The last of type is :nth-last-of-type(1), not 0.
Tag must match — div:nth-last-of-type(1) will not match a <p> element.
Overlapping rules — Multiple :nth-last-of-type() rules can apply to the same element; watch specificity.
A11y
♿ Accessibility
Contrast on highlights — Emphasized last paragraphs must still meet WCAG contrast requirements.
Do not rely on color alone — Pair visual cues with clear headings or labels where meaning matters.
Semantic markup — Use proper <p>, <ul>, and heading tags for screen readers.
Reading order — Styling the last paragraph does not change document order for assistive technology.
🧠 How :nth-last-of-type() Works
1
Filter by element type
The browser gathers only siblings matching the tag before :nth-last-of-type().
Type filter
2
Number from the end
Those siblings are indexed starting at 1 from the last element of that type.
Reverse index
3
Evaluates n expression
The value inside :nth-last-of-type() is tested against each reverse position.
Match
=
📊
Type-aware reverse styling
Last paragraphs, last list items, and last-N patterns work even with mixed sibling tags.
Compatibility
🖥 Browser Compatibility
:nth-last-of-type() is supported in all modern browsers and has been available since CSS3 Selectors.
✓ Baseline · Universal support
Type-based reverse selectors everywhere
:nth-last-of-type() works reliably in Chrome, Firefox, Safari, Edge, and Opera.
99%Universal support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll modern versions
Full support
:nth-last-of-type() pseudo-class99% supported
Bottom line::nth-last-of-type() is safe for articles, lists, and mixed-content layouts in all modern projects.
Wrap Up
🎉 Conclusion
The CSS :nth-last-of-type() selector provides precise control over elements at the end of a sibling group when tag type matters. It is the reverse, type-aware counterpart to :nth-of-type().
From highlighting the last paragraph in an article to selecting the last two headings with -n+2, mastering :nth-last-of-type() helps you write cleaner stylesheets for real-world mixed HTML.
Choose nth-last-of-type when mixed tags break nth-last-child
Scope with parent selectors like article > p:nth-last-of-type()
Use :last-of-type when you only need the final element of a type
Test formulas with n=0,1,2 before deploying
❌ Don’t
Expect it to count across different tag types
Confuse it with nth-last-child on mixed layouts
Assume counting starts at 0
Omit the element type from the selector
Rely on highlight color alone for meaning
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :nth-last-of-type()
Use these points when styling from the end by element type.
5
Core concepts
type01
Same tag only
Counts by type.
Purpose
←102
Reverse count
1 = last of type.
Syntax
-n+203
Last N of type
Negative step.
Pattern
≠04
vs nth-last-child
Mixed tags.
Compare
🌐05
99% support
All browsers.
Compat
❓ Frequently Asked Questions
The :nth-last-of-type() pseudo-class matches elements of a specific tag type based on their position among siblings of that same type, counting from the end. You pass a number, odd, even, or an an+b formula inside the parentheses.
Yes. :nth-last-of-type(1) selects the last sibling of that element type, which is identical to :last-of-type. Use whichever reads more clearly in your stylesheet.
:nth-last-child() counts all sibling children from the end regardless of tag type. :nth-last-of-type() counts only siblings of the same element type from the end, ignoring other tags in between.
Use p:nth-last-of-type(-n+3). The negative step selects from the end among p elements only: the last p, second-to-last p, and third-to-last p.
Yes. All modern browsers support :nth-last-of-type(). It has been available since CSS3 Selectors and works reliably for articles, lists, and mixed-content layouts.