The :last-of-type pseudo-class matches the last sibling of a specific tag type inside its parent. It is essential when mixed elements appear after your target.
01
Last of type
By tag name.
02
p:last-of-type
Last paragraph.
03
Lists
Last li item.
04
Mixed tags
Ignores others.
05
Combine
div > p:last-of-type.
06
vs :last-child
Know the diff.
Fundamentals
Introduction
The :last-of-type selector in CSS is a structural pseudo-class that selects the last element of a specific type within its parent.
It is particularly useful when you need to target and style the last occurrence of a particular element without adding extra classes or IDs.
Definition and Usage
Write the element selector before the pseudo-class: p:last-of-type or li:last-of-type. The rule matches the last sibling of that tag type, regardless of what element types come after it.
💡
Beginner Tip
When a footer <div> appears after your paragraphs, p:last-child will not match any paragraph — but p:last-of-type still styles the final paragraph. This is the most common reason to choose :last-of-type.
Foundation
📝 Syntax
The syntax for the :last-of-type pseudo-class is:
syntax.css
element:last-of-type{/* CSS properties */}
element can be any valid HTML tag such as div, p, or li. The selector applies styles to the last element of that type among its siblings.
Matches the last sibling of the specified tag type inside the parent.
Other element types before or after do not block the match.
Combine with combinators: section > p:last-of-type for direct children.
Tag in the selector must match the element you want to style.
Do not confuse with :last-child, which requires being the last child overall.
:last-of-type vs :last-child
Selector
Matches when…
p:last-of-type
The <p> is the last <p> among siblings, even if a <div> comes after
p:last-child
The <p> is the last child of its parent (no element after it)
Related Selectors
:first-of-type — matches the first element of a given tag among siblings
:last-child — matches the last child of a parent (any type)
:nth-of-type() — matches elements of a type by index
> child combinator — limit matches to direct children
Cheat Sheet
⚡ Quick Reference
Question
Answer
Selector type
Structural pseudo-class
When it applies
Element is the last of its tag type among siblings
Index basis
DOM order among same-tag siblings
Common use
Last paragraph, last list item, closing emphasis
Typical pattern
article > p:last-of-type { margin-bottom: 0; }
Browser support
All modern browsers
Context
When to Use :last-of-type
:last-of-type is ideal when tag type matters more than overall position:
Article closings — Emphasize the final paragraph even when a footer div follows.
List items — Style the last <li> in a navigation menu.
Mixed content blocks — Target the last heading or paragraph among varied siblings.
Spacing resets — Remove bottom margin from the last paragraph in a section.
Table columns — Style the last <td> in a row pattern.
Preview
👀 Live Preview
The last <p> and last <li> match :last-of-type:
This is the first paragraph.
This is the second paragraph.
This is the last paragraph.
Item 1
Item 2
Item 3 (last)
Hands-On
Examples Gallery
Practice :last-of-type with paragraphs, lists, scoped selectors, and comparison with :last-child.
📜 Core Patterns
Style the last element of a specific tag type inside a parent.
Example 1 — Style the last paragraph
Make the final <p> in a section stand out with bold red text.
last-of-type-paragraph.html
<style>p:last-of-type{color:#b91c1c;font-weight:700;}</style><section><p>This is the first paragraph.</p><p>This is the second paragraph.</p><p>This is the last paragraph.</p></section>
The combinator limits the search to paragraphs inside each <div>. Only the last <p> in that div gets the border and zero bottom margin.
Example 4 — :last-of-type vs :last-child
When a footer <div> is last, p:last-of-type still matches but p:last-child does not.
last-of-type-vs-child.css
/* Matches the last p — works here */p:last-of-type{border-left:4px solid #2563eb;padding-left:0.75rem;}/* Does not match — div is the last child */p:last-child{border-left:4px solid #ef4444;}
The footer <div> is the last child, so no paragraph matches p:last-child. p:last-of-type ignores the div and styles the final paragraph.
Tips
💬 Usage Tips
Any element type — Works with paragraphs, list items, headings, table cells, and more.
Combine selectors — Use div p:last-of-type to target the last paragraph inside each div.
Mixed siblings — Prefer :last-of-type when footers, asides, or other tags follow your target.
Pair with :first-of-type — Style opening and closing elements in the same block symmetrically.
No extra markup — Avoid adding classes like .last when structure is predictable.
Watch Out
⚠️ Common Pitfalls
Confusing with :last-child — :last-of-type selects the last of a tag type, not the last child overall.
Wrong tag in selector — p:last-of-type will not match a <span>; the tag must match.
Multiple same-type siblings — Only the final one of that type matches, not every last-in-group if nested differently.
Expecting last child — Use :last-child when you need the absolute last element regardless of type.
Dynamic content — JavaScript-inserted elements of the same type can change which node matches.
A11y
♿ Accessibility
Do not rely on position alone — Visual emphasis on the last item should not be the only way to convey importance.
Semantic markup — Use proper heading and list structure; CSS position styling does not add meaning for assistive tech.
Color contrast — Ensure highlight colors on the last paragraph or list item meet WCAG requirements.
Focus order — Styling the last link in a menu does not change keyboard tab sequence.
🧠 How :last-of-type Works
1
Browser finds candidate elements
CSS gathers elements matching the tag before :last-of-type.
Match
2
Filters by tag type
Among siblings, only elements of that tag type are considered.
Filter
3
Picks the last one
The final sibling of that type receives the styles, even if other tags follow.
Select
=
📄
Type-aware styling
The last paragraph, list item, or heading gets closing emphasis or spacing fixes.
Compatibility
🖥 Browser Compatibility
The :last-of-type pseudo-class is supported in all modern browsers and has been available since CSS3 Selectors.
✓ Baseline · Universal support
Structural selectors everywhere
: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
:last-of-type pseudo-class99% supported
Bottom line::last-of-type is safe for articles, lists, and mixed-content layouts in all modern projects.
Wrap Up
🎉 Conclusion
The :last-of-type selector allows clean, dynamic styling by targeting the last instance of a particular element type within its parent.
It simplifies design patterns where you need unique closing styles without modifying HTML. When mixed siblings follow your target, prefer :last-of-type over :last-child.
Use p:last-of-type when footers or divs follow paragraphs
Pair with :first-of-type for symmetric block styling
Inspect sibling order when styles do not apply
Combine with child combinator > for direct children
Choose :last-child when you need the absolute last element
❌ Don’t
Confuse :last-of-type with :last-child
Use the wrong tag in the selector
Assume every last visual element is :last-of-type
Rely on position alone for meaning or hierarchy
Overuse when a semantic class is clearer for authors
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :last-of-type
Use these points when styling by element type.
5
Core concepts
Last01
Last of type
By tag name.
Purpose
📝02
Syntax
p:last-of-type.
Pattern
📄03
Mixed tags
Still matches.
Use case
≠04
vs last-child
Different rules.
Compare
🌐05
99% support
All browsers.
Compat
❓ Frequently Asked Questions
The :last-of-type pseudo-class matches an element when it is the last sibling of its tag type inside its parent. Other element types appearing after it do not prevent a match.
Write the element selector before the pseudo-class: p:last-of-type matches the last paragraph among siblings, even if a div or footer element comes after it.
:last-child requires the element to be the last child of any type. :last-of-type matches the last element of that tag name among siblings, even if another element type comes after it.
Usually because there is another element of the same tag type after your target, or the selector tag does not match the element. Check sibling order and tag names in DevTools.
Yes. All modern browsers support :last-of-type. It has been available since CSS3 Selectors and works reliably for lists, articles, and layout patterns.