The :only-child pseudo-class matches an element that has no element siblings — it is the sole child of its parent.
It is the same idea as :first-child:last-child, with a clearer name and lower specificity. Compare with :only-of-type, which allows siblings of other tags.
Baseline · Widely available.
Not deprecated. CSS3 structural pseudo-class. Extremely broad browser support.
Syntax
CSS Syntax
syntax.css
:only-child{ /* declarations */ }
Often typed as li:only-child, p:only-child, or .card:only-child.
Key point. Any element sibling blocks :only-child.
Example 3 — Nested Only Child
An only child can sit deep inside a larger tree.
CSS
<style>
div:only-child {
color: #dc2626;
}
</style>
<div>
<div>I am an only child.</div>
</div>
<div>
<div>Sibling A</div>
<div>Sibling B
<div>Nested only child</div>
</div>
</div>
Inner match. Each check is against that element’s own parent.
Top example. One child div — red text.
Siblings A/B. Not only-children of the outer box.
Deep nest. The innermost div is still an only child of Sibling B.
Edge Cases & Pitfalls
If the rule never fires, check these first.
siblings
Any element sibling blocks it
A hidden span, icon wrapper, or extra div next to your target means it is not an only child.
vs type
:only-child ≠ :only-of-type
:only-of-type allows other tags beside it. Use that when you mean “only p among mixed siblings.”
equiv
Same as first + last
:only-child equals :first-child:last-child, with lower specificity. Prefer the shorter name.
text
Text nodes do not count
Whitespace or comments next to a single element child still allow :only-child to match.
parent
Checked per parent
Nested only-children are common. Each element is judged against its own parent, not the whole page.
Compatibility
Browser Support
The :only-child pseudo-class is a browser CSS selector. Node.js, Deno, and Bun do not apply it.
✓ Baseline · Widely available
:only-child
Supported in all modern browsers (CSS3). Safe for teaching and production.
100%Core support
Google Chrome2+
Full support
Mozilla Firefox1.5+
Full support
Apple Safari3.1+
Full support
Microsoft Edge12+
Full support
Opera9.5+
Full support
Samsung Internet1+
Full support
Internet Explorer9+
Full support
:only-childUniversal
Bottom line: No prefixes. No polyfills. It is not a runtime API.
Remember
Key Takeaways
Role: match an element with no element siblings.
Shorthand: same idea as :first-child:last-child.
vs type: use :only-of-type when other tags may sit beside it.
Tip: check the parent’s element children in DevTools.
One line::only-child styles the element that stands alone under its parent.
Frequently Asked Questions
It matches an element that has no element siblings — it is the sole child of its parent.
Yes. Being both first and last child means it is the only child. :only-child is the clearer shorthand and has lower specificity than chaining those two.
:only-child requires zero 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-child.
The parent almost always has another element child. Check DevTools for sibling tags you forgot about.
Yes. It is Baseline Widely available (CSS3). It is a browser selector, not a Node.js or runtime API.