The :only-of-type pseudo-class targets an element that is the only one of its tag type among siblings — other element types may still be present.
01
By type
Same tag only.
02
Solo p, h2, li
Type patterns.
03
vs only-child
Know the diff.
04
= first:last
Of that type.
05
Mixed tags
Div + p OK.
06
Articles
Solo headings.
Fundamentals
Introduction
The :only-of-type selector in CSS targets an element that is the only one of its type within its parent. This pseudo-class is useful when you need to style a single paragraph, heading, or list item of a given tag without requiring it to be the only child overall.
It enhances precision in mixed-content layouts where headings, paragraphs, and divs appear together as siblings.
Definition and Usage
Write element:only-of-type to match elements that are the sole sibling of their tag type inside a parent.
💡
Beginner Tip
:only-of-type is equivalent to :first-of-type:last-of-type. A lone <p> beside a <div> matches p:only-of-type but notp:only-child because the div is a sibling.
Foundation
📝 Syntax
The syntax for the :only-of-type pseudo-class is:
syntax.css
element:only-of-type{/* CSS properties */}
element is the tag you want to style (e.g. p, h2, li).
The rule applies only when no other sibling of that same tag type exists in the parent.
The first container has one <p>, so it matches. The second has two paragraph siblings of the same type, so neither matches :only-of-type.
Example 2 — Paragraph beside a div sibling
Show the key difference from :only-child — a <div> sibling does not block p:only-of-type.
only-of-type-mixed.html
<style>p:only-of-type{background:#dcfce7;border-left:4px solid #16a34a;padding:0.5rem 0.65rem;}</style><article><p>Lone paragraph in this section.</p><divclass="footer">Share link</div></article>
When a nav renders a single list item, li:only-of-type highlights it without adding a utility class. Add a second <li> and the rule stops applying.
Tips
💬 Usage Tips
Prefer over only-child in mixed layouts — When other tag types may sit as siblings.
Scope with parents — article > p:only-of-type limits to direct paragraph children.
Dynamic content — Great for CMS output where section structure varies.
Check same-type siblings — A second paragraph blocks the match, not a div.
Combine with :hover — p:only-of-type:hover for interactive solo elements.
Watch Out
⚠️ Common Pitfalls
Multiple elements of same type — Two <p> siblings means neither matches p:only-of-type.
Confusing with only-child — A lone <p> beside a <div> is not :only-child.
Wrong tag in selector — li:only-of-type will not match a solo <div>.
Expecting match across parents — The check is among siblings inside one parent only.
Specificity conflicts — General element rules may override unless ordered correctly.
A11y
♿ Accessibility
Contrast on highlights — Solo-type emphasis must still meet WCAG contrast requirements.
Do not rely on color alone — Pair visual cues with clear text or structure.
Semantic markup — Use proper heading, paragraph, and list tags for screen readers.
Reading order unchanged — Positional styling does not affect assistive technology order.
🧠 How :only-of-type Works
1
Filter by element type
The browser gathers siblings matching the tag before :only-of-type.
Type filter
2
Counts same-type siblings
Only elements of that tag type are counted; other types are ignored.
Type count
3
Verifies count equals 1
The selector matches only when exactly one sibling of that type exists.
Count = 1
=
👤
Type-unique styling
Lone paragraphs, headings, and list items get special treatment in mixed layouts.
Compatibility
🖥 Browser Compatibility
:only-of-type is supported in all modern browsers and has been available since CSS3 Selectors.
✓ Baseline · Universal support
Structural selectors everywhere
:only-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
:only-of-type pseudo-class99% supported
Bottom line::only-of-type is safe for articles, lists, and mixed-content layouts in all modern projects.
Wrap Up
🎉 Conclusion
The CSS :only-of-type selector provides precise control over elements that are unique of their tag type within a parent. It is especially valuable in mixed-content layouts where :only-child would be too restrictive.
From solo paragraphs beside footer divs to lone headings in articles, mastering :only-of-type helps you write cleaner, more adaptive stylesheets.
Scope with parent selectors like article > p:only-of-type
Check DevTools when a rule does not apply
Prefer over only-child in mixed HTML structures
Combine with :hover for interactive solo elements
❌ Don’t
Expect a match when two siblings share the same tag
Confuse :only-of-type with :only-child
Target the wrong element type in the selector
Assume it checks across multiple parents
Rely on highlight color alone for meaning
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :only-of-type
Use these points when styling type-unique elements.
5
Core concepts
type01
Sole of type
One tag type.
Purpose
=02
first:last
Of that type.
Syntax
≠03
vs only-child
Type vs all.
Compare
p04
Mixed tags
Div + p OK.
Pattern
🌐05
99% support
All browsers.
Compat
❓ Frequently Asked Questions
The :only-of-type pseudo-class matches an element only when it is the sole element of its tag type among its siblings. Other sibling elements of different types do not prevent a match.
Yes. An element that is both the first and last of its type among siblings matches :only-of-type. It is shorthand for that combination.
:only-of-type checks only the element's tag type among siblings. :only-child requires the element to have no siblings of any type. A lone p next to a div is :only-of-type but not :only-child.
Usually because another sibling of the same tag type exists. Two paragraphs in the same parent means neither matches p:only-of-type. Check sibling elements of the same tag in DevTools.
Yes. All modern browsers support :only-of-type. It has been available since CSS3 Selectors and works reliably for articles, lists, and mixed-content layouts.