Notes
Single paragraph

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.
Not deprecated. CSS3 structural pseudo-class. Extremely broad browser support (Baseline since July 2015).
:only-of-type { /* declarations */ }Often typed as p:only-of-type, img:only-of-type, or a:only-of-type.
Style an element when it is the unique tag of its kind under a parent.
p, img, a, …).tag:only-of-type (or bare :only-of-type).:only-child instead if you need zero siblings of any kind.| Situation | Matches p:only-of-type? |
|---|---|
One p under the parent | Yes |
One p plus a sibling div | Yes |
Two or more p siblings | No |
Whitespace / comments beside one p | Yes |
Switch between one paragraph and two. Only the lone p matches p:only-of-type — even with a footer sibling.
Each snippet focuses on :only-of-type. Use Try It Yourself for a live editor.
Highlight a paragraph only when it is the unique p under its parent.
<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> p — matches p:only-of-type.p siblings — neither matches.A lone p still matches when a different tag sits beside it.
<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> p + one div — the p still matches.p tags — no match, footer irrelevant.p would not match :only-child.Each parent is checked on its own. A nested sole em can match while sibling ems elsewhere do not.
<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> em inside the p — red text.em siblings — neither matches.i / em demo.If the rule never fires, check these first.
Two p elements under one parent means neither is p:only-of-type.
:only-of-type ≠ :only-child:only-of-type allows other tags. :only-child requires a sole element child.
:only-of-type equals :first-of-type:last-of-type, with lower specificity. Prefer the shorter name.
Whitespace or comments next to a lone element of that type still allow a match.
Nested sole elements of a type are common. Each element is judged against its own parent’s siblings.
The :only-of-type pseudo-class is a browser CSS selector. Node.js, Deno, and Bun do not apply it.
Supported in all modern browsers since July 2015 (CSS3). Safe for teaching and production.
Bottom line: No prefixes. No polyfills. It is not a runtime API.
:first-of-type:last-of-type.:only-child when no element siblings are allowed.One line: :only-of-type styles the element that is unique among siblings of its tag.
Learn how to style form fields that are not required.
8 people found this page helpful