CSS :only-child Pseudo-class

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

CSS Syntax

syntax.css
:only-child { /* declarations */ }

Often typed as li:only-child, p:only-child, or .card:only-child.

Selector Forms

PatternMatchesTry it
li:only-childA list with exactly one itemTry it
p:only-childA paragraph that is the sole childTry it
div:only-childNested sole boxes inside parentsTry it
vs :only-of-typeOnly-of-type allows other tag siblingsTry it

How to Use :only-child

Style the child when it stands alone under its parent.

  1. Identify the parent that might have one child.
  2. Write :only-child or a typed form like li:only-child.
  3. Confirm there are no other element siblings.
  4. Use :only-of-type instead if other tags are allowed beside it.
SituationMatches :only-child?
Parent has one element childYes
Parent has two or more element childrenNo
Whitespace / comments beside one elementYes
One p plus a sibling spanNo

Live Preview

Switch between a single-item list and a multi-item list. Only the lone li matches :only-child.

Pink text = li:only-child match.

List
  • Only item

One item — the li matches :only-child

CSS Examples

Each snippet focuses on :only-child. Use Try It Yourself for a live editor.

Example 1 — Style a Lone List Item

Highlight lists that contain exactly one item.

CSS
<style>
  li:only-child {
    color: #c026d3;
    font-weight: 600;
  }
</style>

<ul>
  <li>Only item</li>
</ul>
<ul>
  <li>One</li>
  <li>Two</li>
</ul>
Try It Yourself

How It Works

  1. First list. One li — matches li:only-child.
  2. Style. Magenta weight draws attention to the lone item.
  3. Second list. Two siblings — neither is an only child.
  4. Use case. Special treatment for single-result lists.

Example 2 — Sole Paragraph in a Box

Style a paragraph only when it is the parent’s only child.

CSS
<style>
  p:only-child {
    background: #fce7f3;
    padding: 0.5rem;
  }
</style>

<div>
  <p>Alone in this box</p>
</div>
<div>
  <p>Has a sibling</p>
  <span>Extra</span>
</div>
Try It Yourself

How It Works

  1. First box. One child p — matches.
  2. Background. Marks the alone paragraph.
  3. Second box. p + span siblings — no match.
  4. 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>
Try It Yourself

How It Works

  1. Inner match. Each check is against that element’s own parent.
  2. Top example. One child div — red text.
  3. Siblings A/B. Not only-children of the outer box.
  4. 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.

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 Chrome 2+
Full support
Mozilla Firefox 1.5+
Full support
Apple Safari 3.1+
Full support
Microsoft Edge 12+
Full support
Opera 9.5+
Full support
Samsung Internet 1+
Full support
Internet Explorer 9+
Full support
:only-child Universal

Bottom line: No prefixes. No polyfills. It is not a runtime API.

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.

Next: :only-of-type Pseudo-class

Continue through the CSS tutorial reference.

:only-of-type Pseudo-class tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful