CSS :only-child Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Structural

What You’ll Learn

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.

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>.

📝 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.

When It Matches

Parent HTMLDoes p:only-child match?
<div><p>Solo</p></div>Yes
<div><p>A</p><p>B</p></div>No (two children)
<div><p>A</p><span>B</span></div>No (span is a sibling)
<div><span>Note</span></div>No (tag is span, not p)
p:only-child li:only-child .card > *:only-child p:only-child:hover

Related Selectors

  • :only-of-type — sole element of its tag type among siblings (other types allowed)
  • :first-child / :last-child — position among all siblings
  • :empty — parent has no children at all (opposite scenario)
  • :not() — combine for exclusions like li:not(:only-child)

⚡ Quick Reference

QuestionAnswer
Selector typeStructural pseudo-class
Syntaxelement:only-child
Matches whenElement is the sole direct child of its parent
Equivalent to:first-child:last-child
Common useSolo card content, single list item, lone form field
Browser supportAll modern browsers

When to Use :only-child

:only-child is ideal when an element may appear alone or with siblings:

  • Card layouts — Center or emphasize content when a card has just one child element.
  • Dynamic lists — Style a list differently when it contains only one item.
  • Form groups — Apply special spacing when a field wrapper has a single input child.
  • Conditional styling — Avoid extra classes when sibling count determines appearance.
  • Hover effects — Combine with :hover for interactive solo elements.

👀 Live Preview

The solo paragraph gets blue bold styling via p:only-child. The box with two paragraphs is unaffected:

Solo paragraph in this box.

First paragraph.

Second paragraph.

Examples Gallery

Practice :only-child with solo paragraphs, list items, card layouts, and hover combinations.

📜 Core Patterns

Style elements that are the only child inside their parent.

Example 1 — Style the only paragraph in a container

Apply blue bold text to p:only-child when a container holds just one paragraph.

only-child-basic.css
p:only-child {
  color: #1d4ed8;
  font-weight: 700;
}

p {
  color: #334155;
  font-size: 1rem;
}
Try It Yourself

How It Works

The first container has one child, so its paragraph matches. The second container has two paragraph siblings, so neither matches :only-child.

Example 2 — Style a list with one item

Give a solo list item distinct styling when li:only-child applies.

only-child-list.css
li:only-child {
  background: #dcfce7;
  border-left: 4px solid #16a34a;
  padding: 0.65rem 0.9rem;
  border-radius: 0.35rem;
}
Try It Yourself

How It Works

When a navigation list renders a single item, li:only-child lets you style it without a special class. Add a second item and the rule stops applying.

📄 Layouts & Interactions

Use :only-child for card centering and combined pseudo-class effects.

Example 3 — Center content in a solo-child card

When a card wrapper has one child, center it with flexbox triggered by :only-child.

only-child-card.css
.card:has(> :only-child) {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 6rem;
}

.card > p:only-child {
  text-align: center;
  color: #1d4ed8;
}
Try It Yourself

How It Works

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.

only-child-hover.css
p:only-child {
  padding: 0.65rem 0.9rem;
  border-radius: 0.4rem;
  transition: background 0.2s ease;
}

p:only-child:hover {
  background: #fef3c7;
}
Try It Yourself

How It Works

The hover background applies only to paragraphs that are the only child of their parent. Paragraphs with siblings never receive the hover style.

💬 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-classesp: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.

⚠️ 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 selectorli:only-child will not match a solo <div> child.

♿ 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.

🖥 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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
:only-child pseudo-class 99% supported

Bottom line: :only-child is safe for cards, lists, and conditional layouts in all modern projects.

🎉 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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about :only-child

Use these points when styling sole children.

5
Core concepts
= 02

first:last

Equivalent.

Syntax
03

vs only-of-type

All vs type.

Compare
p 04

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.

Practice in the Live Editor

Open the HTML editor and experiment with :only-child, solo cards, and hover combinations.

HTML Editor →

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.

5 people found this page helpful