CSS :last-of-type Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Structural Selectors

What You’ll Learn

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.

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.

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

Basic Example

last-of-type-basic.css
p:last-of-type {
  color: #b91c1c;
  font-weight: 700;
}

li:last-of-type {
  background: #fef9c3;
  font-style: italic;
}
p:last-of-type li:last-of-type h2:last-of-type div p:last-of-type

Syntax Rules

  • 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

SelectorMatches when…
p:last-of-typeThe <p> is the last <p> among siblings, even if a <div> comes after
p:last-childThe <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

⚡ Quick Reference

QuestionAnswer
Selector typeStructural pseudo-class
When it appliesElement is the last of its tag type among siblings
Index basisDOM order among same-tag siblings
Common useLast paragraph, last list item, closing emphasis
Typical patternarticle > p:last-of-type { margin-bottom: 0; }
Browser supportAll modern browsers

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.

👀 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)

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>
Try It Yourself

How It Works

Among the three paragraphs, only the third is the last <p> sibling. Earlier paragraphs have another <p> after them, so they do not match.

Example 2 — Highlight the last list item

Apply a yellow background and italic text to the final <li>.

last-of-type-list.css
li:last-of-type {
  background: #fef9c3;
  font-style: italic;
  color: #854d0e;
}
Try It Yourself

How It Works

li:last-of-type targets the last list item of type li inside the <ul>, regardless of whether other non-li elements exist.

📄 Specificity & Comparison

Scope to containers and understand when :last-of-type beats :last-child.

Example 3 — Last paragraph inside each div

Use div p:last-of-type to target the closing paragraph in every content block.

last-of-type-scoped.css
div p:last-of-type {
  margin-bottom: 0;
  padding-bottom: 0.75rem;
  border-bottom: 2px solid #e2e8f0;
}

div p {
  margin-bottom: 0.75rem;
}
Try It Yourself

How It Works

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;
}
Try It Yourself

How It Works

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.

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

⚠️ Common Pitfalls

  • Confusing with :last-child:last-of-type selects the last of a tag type, not the last child overall.
  • Wrong tag in selectorp: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.

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

🖥 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 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
Full support
Opera All modern versions
Full support
:last-of-type pseudo-class 99% supported

Bottom line: :last-of-type is safe for articles, lists, and mixed-content layouts in all modern projects.

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

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about :last-of-type

Use these points when styling by element type.

5
Core concepts
📝 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.

Practice in the Live Editor

Open the HTML editor and experiment with :last-of-type, paragraphs, and list patterns.

HTML Editor →

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.

5 people found this page helpful