CSS :last-of-type Pseudo-class

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The :last-of-type pseudo-class matches an element that is the last of its tag name among sibling elements.

Example: p:last-of-type styles the last p under a parent, even if a div footer comes after it. Same match as :nth-last-of-type(1). For “must be last child of any type,” use :last-child.

Baseline · Widely available.

Not deprecated. A long-standing structural pseudo-class from Selectors Level 3 / 4.

CSS Syntax

syntax.css
:last-of-type { /* or */ E:last-of-type { … }

Example: p:last-of-type { color: red; font-style: italic; }

Selector Forms

FormDescriptionTry it
E:last-of-typeLast sibling of type E under a parentTry it
dd:last-of-typeLast dd among siblings in a dlTry it
vs :last-childLast of that tag vs must be last child overallTry it
Nestedarticle :last-of-type matches inside each parentTry it
Class + typeLast of each type that also has a classTry it
Spacing trimRemove margin on the last paragraphTry it

How to Use :last-of-type

Pick the tag you care about, then select the last sibling of that type.

  1. Look at siblings under one parent (DevTools helps).
  2. Write E:last-of-type for the last element of type E.
  3. Style that last item (color, border, spacing).
  4. If it must also be the overall last child, use :last-child.
PieceRequired?Role
Parent with siblingsYesMatching is relative to one parent
:last-of-typeYesLast sibling of that element’s tag
Type selectorOptional*Usually written as p:last-of-type; bare :last-of-type implies *

*MDN: with no type written, the universal selector is implied.

Live Preview

Toggle between p:last-of-type and p:last-child on the same markup (paragraphs, then a footer).

Compare selectors

With a footer div last, only p:last-of-type highlights a paragraph.

Article

Heading

Paragraph 1

Paragraph 2

p:last-of-type matches Paragraph 2.

CSS Examples

Each snippet focuses on :last-of-type. Extra styles stay minimal for study. Use Try It Yourself for a live editor.

Example 1 — Last Paragraph Before a Footer

Style the last p even when a div comes after it.

CSS
<style>
  p:last-of-type {
    color: red;
    font-style: italic;
  }
</style>

<h2>Heading</h2>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<div>Footer note</div>
Try It Yourself

How It Works

  1. Siblings. The heading, paragraphs, and footer share one parent.
  2. Last of type. Among p siblings, Paragraph 2 is last.
  3. Footer ignored for type. The div does not stop p:last-of-type.
  4. Contrast. p:last-child would match nothing here.

Example 2 — Last dd in a List

Highlight the last definition in a dl.

CSS
<style>
  dt {
    font-weight: bold;
  }

  dd:last-of-type {
    border: 2px solid orange;
  }
</style>

<dl>
  <dt>Vegetables:</dt>
  <dd>Tomatoes</dd>
  <dd>Cucumbers</dd>
  <dt>Fruits:</dt>
  <dd>Apples</dd>
  <dd>Oranges</dd>
</dl>
Try It Yourself

How It Works

  1. Same parent. All dt/dd share the dl.
  2. Last dd only. dd:last-of-type matches the last dd in that sibling list (Oranges).
  3. Earlier dds. Tomatoes, Cucumbers, and Apples are not the last of type.
  4. Note. There is only one “last dd” under the whole dl—not one per dt.

Example 3 — Nested Last-of-Type

Each parent can have its own last-of-type match.

CSS
<style>
  article :last-of-type {
    background-color: pink;
  }
</style>

<article>
  <div>This div is first.</div>
  <div>This <span>nested span is last</span>!</div>
  <div>This <em>nested em is first</em>, but this <em>nested em is last</em>!</div>
  <p>This p qualifies!</p>
  <div>This is the final div!</div>
</article>
Try It Yourself

How It Works

  1. Descendant selector. article :last-of-type looks for last-of-type under article.
  2. Outer siblings. The final div and the only p match among article children.
  3. Nested. The last span and last em match inside their parent divs.
  4. Implied *. Writing :last-of-type without a tag means any type.

Edge Cases & Pitfalls

Confusion with :last-child causes most mistakes.

vs child

Not the same as :last-child

Need the absolute last child of any tag? Use :last-child.

dl

One last dd per parent

dd:last-of-type matches the last dd among all siblings in the dl, not the last under each dt.

alias

Same as :nth-last-of-type(1)

:last-of-type and :nth-last-of-type(1) select the same elements.

scope

Per parent, not per page

Each parent has its own sibling list. Nested containers can each match.

only

Single of a type also matches

The only p under a parent is still the last of type. For “exactly one of that type,” use :only-of-type.

Browser Support

:last-of-type is a browser CSS selector. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available

:last-of-type

Supported in all modern browsers (Chrome 1+, Firefox 3.5+, Safari 3.2+, Edge 12+, Opera 9.5+, IE 9+).

97% Global support
Google Chrome 1+
Full support
Mozilla Firefox 3.5+
Full support
Apple Safari 3.2+
Full support
Microsoft Edge 12+
Full support*
Opera 9.5+
Full support
Samsung Internet 1+
Full support
Internet Explorer 9+
Full support*
:last-of-type Widely supported

Bottom line: *IE and very old Edge treated unknown/custom tags as one type. Safe for standard HTML on modern sites. It is not a JavaScript runtime API.

Key Takeaways

  • Role: match the last sibling of a given tag under a parent.
  • Form: E:last-of-type = last element of tag E.
  • vs :last-child: of-type still matches when another tag follows.
  • Alias: same match as :nth-last-of-type(1).

One line: :last-of-type styles the last sibling of a tag, even if other element types come after it.

Frequently Asked Questions

It matches an element that is the last sibling of its tag type inside its parent. Other element types after it do not block the match.
:last-child requires the element to be the last child of any type. :last-of-type matches the last p, last li, or last div of that tag among siblings, even if another tag comes after it.
Yes. If the parent contains paragraphs then a div footer, p:last-of-type matches the last p even though the div is the overall last child.
Yes. :last-of-type matches the same elements as :nth-last-of-type(1).
Yes. Each parent has its own sibling group. article :last-of-type can match inside every nested parent, not only once on the page.
No. It is a CSS selector evaluated by the browser layout engine, not a Node.js or server runtime feature.

Next: :link

Style unvisited hyperlinks with the :link pseudo-class.

:link 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.

8 people found this page helpful