CSS :only-of-type Pseudo-class

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

Definition and Usage

The :only-of-type pseudo-class matches an element that has no siblings of the same tag name.

Other tags beside it are fine. It equals :first-of-type:last-of-type. Compare with :only-child, which allows no element siblings at all.

Baseline · Widely available.

Not deprecated. CSS3 structural pseudo-class. Extremely broad browser support (Baseline since July 2015).

CSS Syntax

syntax.css
:only-of-type { /* declarations */ }

Often typed as p:only-of-type, img:only-of-type, or a:only-of-type.

Selector Forms

PatternMatchesTry it
p:only-of-typeThe sole paragraph among siblingsTry it
p:only-of-type + other tagsLone p next to a div still matchesTry it
em:only-of-typeNested sole element of a typeTry it
vs :only-childOnly-child forbids any tag siblingsTry it

How to Use :only-of-type

Style an element when it is the unique tag of its kind under a parent.

  1. Pick the tag you care about (p, img, a, …).
  2. Write tag:only-of-type (or bare :only-of-type).
  3. Confirm no other sibling uses that same tag.
  4. Use :only-child instead if you need zero siblings of any kind.
SituationMatches p:only-of-type?
One p under the parentYes
One p plus a sibling divYes
Two or more p siblingsNo
Whitespace / comments beside one pYes

Live Preview

Switch between one paragraph and two. Only the lone p matches p:only-of-type — even with a footer sibling.

Blue text = p:only-of-type match. Footer is always present.

Article

Notes

Single paragraph

One p — matches p:only-of-type (footer does not block it)

CSS Examples

Each snippet focuses on :only-of-type. Use Try It Yourself for a live editor.

Example 1 — Style a Sole Paragraph

Highlight a paragraph only when it is the unique p under its parent.

CSS
<style>
  p:only-of-type {
    color: #1d4ed8;
    font-weight: 600;
  }
</style>

<div>
  <p>Only paragraph</p>
</div>
<div>
  <p>First</p>
  <p>Second</p>
</div>
Try It Yourself

How It Works

  1. First box. One p — matches p:only-of-type.
  2. Style. Blue weight marks the unique paragraph.
  3. Second box. Two p siblings — neither matches.
  4. Use case. Special style when a section has a single body paragraph.

Example 2 — Other Tags Do Not Block It

A lone p still matches when a different tag sits beside it.

CSS
<style>
  p:only-of-type {
    background: #dbeafe;
    padding: 0.5rem;
  }
</style>

<div>
  <p>Only p, with a footer sibling</p>
  <div>Footer</div>
</div>
<div>
  <p>First p</p>
  <p>Second p</p>
  <div>Footer</div>
</div>
Try It Yourself

How It Works

  1. First box. One p + one div — the p still matches.
  2. Background. Shows the unique paragraph clearly.
  3. Second box. Two p tags — no match, footer irrelevant.
  4. vs only-child. That first p would not match :only-child.

Example 3 — Nested Only of Type

Each parent is checked on its own. A nested sole em can match while sibling ems elsewhere do not.

CSS
<style>
  em:only-of-type {
    color: #dc2626;
  }
</style>

<div>
  <p>Hello <em>world</em></p>
</div>
<div>
  <p><em>One</em> and <em>two</em></p>
</div>
Try It Yourself

How It Works

  1. Parent scope. Matching is judged among siblings of the same parent.
  2. First line. One em inside the p — red text.
  3. Second line. Two em siblings — neither matches.
  4. Tip. Same idea as MDN’s nested i / em demo.

Edge Cases & Pitfalls

If the rule never fires, check these first.

same tag

Another same-tag sibling blocks it

Two p elements under one parent means neither is p:only-of-type.

vs child

:only-of-type ≠ :only-child

:only-of-type allows other tags. :only-child requires a sole element child.

equiv

Same as first + last of type

:only-of-type equals :first-of-type:last-of-type, with lower specificity. Prefer the shorter name.

text

Text nodes do not count

Whitespace or comments next to a lone element of that type still allow a match.

parent

Checked per parent

Nested sole elements of a type are common. Each element is judged against its own parent’s siblings.

Browser Support

The :only-of-type pseudo-class is a browser CSS selector. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

:only-of-type

Supported in all modern browsers since July 2015 (CSS3). Safe for teaching and production.

100% Core support
Google Chrome 1+
Full support
Mozilla Firefox 3.5+
Full support
Apple Safari 3.1+
Full support
Microsoft Edge 12+
Full support
Opera 9.5+
Full support
Samsung Internet 1+
Full support
Internet Explorer 9+
Full support
:only-of-type Universal

Bottom line: No prefixes. No polyfills. It is not a runtime API.

Key Takeaways

  • Role: match the sole sibling of a given tag name.
  • Shorthand: same idea as :first-of-type:last-of-type.
  • vs child: use :only-child when no element siblings are allowed.
  • Tip: count same-tag siblings in DevTools, not every child.

One line: :only-of-type styles the element that is unique among siblings of its tag.

Frequently Asked Questions

It matches an element that has no siblings with the same tag name. Other siblings of different tags do not prevent a match.
Yes. Being both the first and last of its type among siblings means it is the only one of that type. Prefer :only-of-type for clarity and lower specificity.
:only-child requires zero element siblings of any tag. :only-of-type matches when it is the only sibling of its own tag, even if other tags sit beside it.
No. Structural pseudo-classes count element siblings. Whitespace text and comments do not create siblings for :only-of-type.
Another sibling of the same tag usually exists. Two paragraphs under one parent means neither matches p:only-of-type.
Yes. It is Baseline Widely available (CSS3). It is a browser selector, not a Node.js or runtime API.

Next: :optional

Learn how to style form fields that are not required.

:optional 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