The :only-child pseudo-class targets an element that is the sole child of its parent — it has no sibling elements at all.
01
No siblings
Sole child only.
02
p:only-child
Paragraph cards.
03
vs only-of-type
Know the diff.
04
= first:last
Shorthand.
05
Cards & forms
Solo content.
06
+ :hover
Combine rules.
Fundamentals
Introduction
The :only-child selector in CSS is used to target an element that is the only child of its parent. This means the element has no siblings — no other elements share the same parent container.
This pseudo-class is particularly useful when you want to apply special styles to content that stands alone inside a card, panel, or wrapper without adding extra classes or JavaScript.
Definition and Usage
Write element:only-child to match elements that are the sole direct child of their parent. The element type before the pseudo-class filters which tags can match.
💡
Beginner Tip
:only-child is equivalent to :first-child:last-child. If a parent has two or more children, none of them match :only-child — even if only one of them is a <p>.
Foundation
📝 Syntax
The syntax for the :only-child pseudo-class is:
syntax.css
element:only-child{/* CSS properties */}
element is the tag you want to style (e.g. p, li, div).
The rule applies only when that element is the only direct child of its parent.
p:only-child targets the lone paragraph. Pairing with :has(> :only-child) on the parent lets you adjust the card layout when it contains exactly one child.
Example 4 — Hover effect on only-child paragraphs
Combine :only-child with :hover for interactive solo elements.
The hover background applies only to paragraphs that are the only child of their parent. Paragraphs with siblings never receive the hover style.
Tips
💬 Usage Tips
No extra classes needed — Let sibling count drive conditional styling.
Scope with parents — .card > p:only-child limits matches to direct children.
Combine pseudo-classes — p:only-child:hover for interactive solo elements.
Check all sibling types — A single <span> sibling prevents a match.
When type matters more — Use :only-of-type if other tag types may coexist.
Watch Out
⚠️ Common Pitfalls
Expecting match with siblings present — Two or more children means no element matches :only-child.
Confusing with only-of-type — A lone <p> beside a <div> is p:only-of-type but not p:only-child.
Hidden siblings — Elements with display:none still count as siblings in the DOM.
Specificity conflicts — A general p rule may need lower specificity or ordering adjustments.
Wrong tag in selector — li:only-child will not match a solo <div> child.
A11y
♿ Accessibility
Contrast on highlights — Solo-child emphasis must still meet WCAG contrast requirements.
Do not rely on color alone — Pair visual cues with text or icons where meaning matters.
Semantic structure — Use proper list and paragraph markup regardless of styling.
Hover-only cues — Ensure keyboard focus states mirror hover styles where interaction matters.
🧠 How :only-child Works
1
Browser checks parent
The parent element’s direct children are counted.
Sibling count
2
Verifies sole child
The selector matches only when exactly one direct child exists.
Count = 1
3
Filters by element
Only elements matching the tag before :only-child receive styles.
Filter
=
👤
Solo-child styling
Unique elements inside wrappers get special treatment without extra classes.
Compatibility
🖥 Browser Compatibility
:only-child is supported in all modern browsers and has been available since CSS3 Selectors.
✓ Baseline · Universal support
Structural selectors everywhere
:only-child 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-child pseudo-class99% supported
Bottom line::only-child is safe for cards, lists, and conditional layouts in all modern projects.
Wrap Up
🎉 Conclusion
The CSS :only-child selector is a handy tool for styling elements that are the sole child of their parent. It enables clean, conditional styling without extra classes or scripts.
From solo card content to single-item lists and hover interactions, mastering :only-child helps you write flexible stylesheets that adapt to sibling count.
Use for dynamic content that may appear alone or in groups
Scope with parent selectors like .card > p:only-child
Combine with :hover for interactive solo elements
Check DevTools when a rule does not apply
Prefer :only-of-type when other tag types may coexist
❌ Don’t
Expect a match when any sibling exists
Confuse :only-child with :only-of-type
Forget hidden DOM siblings still count
Target the wrong element type in the selector
Rely on highlight color alone for meaning
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :only-child
Use these points when styling sole children.
5
Core concepts
101
Sole child
No siblings.
Purpose
=02
first:last
Equivalent.
Syntax
≠03
vs only-of-type
All vs type.
Compare
p04
Tag filter
Element type.
Pattern
🌐05
99% support
All browsers.
Compat
❓ Frequently Asked Questions
The :only-child pseudo-class matches an element only when it is the sole child of its parent — meaning it has no sibling elements at all.
Yes. An element that is both the first and last child is the only child. :only-child is shorthand for that combination.
:only-child requires the element to have no siblings of any type. :only-of-type matches when it is the only element of its tag type among siblings, even if other tag types are present.
Usually because the element has at least one sibling — another p, div, span, or even a comment-adjacent element in the DOM. Check the parent's direct children in DevTools.
Yes. All modern browsers support :only-child. It has been available since CSS3 Selectors and works reliably for cards, forms, and layout patterns.