jQuery :only-child Selector

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Structural CSS

What You’ll Learn

The :only-child selector matches every element that is the only element child of its parent — no sibling elements at all. Standard CSS since jQuery 1.1.4; ideal for lone buttons, single-item lists, and empty-state UI blocks.

01

Syntax

button:only-child

02

No siblings

Alone in parent

03

Official

div button:only-child

04

1st + last

Both at once

05

vs of-type

Tag-scoped

06

Standard CSS

Native support

Introduction

Some UI blocks contain exactly one actionable element — a single button in a card footer, one list item in a mini menu, or a lone icon in a toolbar cell. jQuery’s :only-child pseudo-class finds those elements by checking whether the parent has any other element children.

Official jQuery documentation is explicit: if the parent has other child elements, nothing is matched. Text nodes do not count — the official demo places the word “Sibling!” next to buttons without blocking a lone button from matching. When two buttons share a parent, neither matches :only-child.

Understanding the :only-child Selector

Think of :only-child as the intersection of first and last among element siblings:

  • li:only-child → the sole li in its ul or ol.
  • Two spans in one div → neither span matches span:only-child.
  • One span plus a p sibling → the span fails — another element child exists.
  • Can match multiple elements document-wide — one per qualifying parent.
💡
Beginner Tip

When you need “the only span among siblings, even if divs are nearby,” use :only-of-type instead. :only-child cares about all element types, not just the tag you prefix.

📝 Syntax

Official jQuery API form (since 1.1.4):

jQuery
jQuery( ":only-child" )

// typical usage — chain after a type selector:
$( "div button:only-child" )
$( "li:only-child" )
$( ".toolbar button:only-child" )

Parameters

  • No arguments — keeps elements with no element siblings under the same parent.

Return value

  • A jQuery object containing every matching element (zero or more).
  • Empty collection when no parent has a lone element child of the selected type.

Official jQuery API example

jQuery
// Label and border each button that is the only child of its div
$( "div button:only-child" )
  .text( "Alone" )
  .css( "border", "2px blue solid" );

⚡ Quick Reference

Parent markupbutton:only-childWhy
<div><button></button></div>MatchesNo element siblings
<div><button></button><button></button></div>No matchTwo button siblings
<div><button></button> text </div>MatchesText nodes ignored
<div><button></button><span></span></div>No matchSpan is an element sibling
Same as both first and last:first-child:last-childEquivalent intersection

📋 :only-child vs :only-of-type and :first-child

All element siblings vs same-tag siblings only.

:only-child
li:only-child

No element siblings

:only-of-type
span:only-of-type

Only span among siblings

:first-child
p:first-child

First, may have siblings

:last-child
p:last-child

Last, may have siblings

Examples Gallery

Example 1 follows the official jQuery div button:only-child demo. Examples 2–5 cover sibling rejection, equivalence with :first-child:last-child, contrast with :only-of-type, and a toolbar styling pattern.

📚 Official jQuery Demo

Rename and border buttons that are the only element child in their div.

Example 1 — Official Demo: div button:only-child

Official jQuery demo — lone buttons become “Alone” with a 2px blue border; buttons beside other element children stay unchanged.

jQuery
$( "div button:only-child" )
  .text( "Alone" )
  .css( "border", "2px blue solid" );
Try It Yourself

How It Works

Each div is evaluated separately. Text like “Sibling!” does not block a match — only element siblings do.

Example 2 — Sibling Rejection: li:only-child

Highlight single-item lists; multi-item lists produce no match.

jQuery
$( "li:only-child" ).css( "backgroundColor", "#dbeafe" );

console.log( "Matches →", $( "li:only-child" ).length );
// 1 from #solo ul, 0 from #multi ul
Try It Yourself

How It Works

Official rule: other child elements in the parent prevent a match — three li siblings means zero :only-child hits.

📈 Practical Patterns

Equivalence, type-scoped contrast, and UI polish.

Example 3 — Equivalence: p:first-child:last-child

When no element siblings exist, :only-child matches the same nodes as combining first and last.

jQuery
console.log( ":only-child →", $( "p:only-child" ).length );
console.log( ":first-child:last-child →", $( "p:first-child:last-child" ).length );
Try It Yourself

How It Works

An only element child is both first and last — useful mental model when debugging selectors.

Example 4 — Contrast: span:only-child vs span:only-of-type

A lone span before a p sibling matches :only-of-type but not :only-child.

jQuery
$( "span:only-child" ).css( "color", "red" );
$( "span:only-of-type" ).css( "backgroundColor", "#fef3c7" );

console.log( "only-child →", $( "span:only-child" ).length );
console.log( "only-of-type →", $( "span:only-of-type" ).length );
Try It Yourself

How It Works

:only-of-type ignores siblings of other tag names; :only-child does not.

Example 5 — Toolbar: .tool-group button:only-child

Style single-action tool groups differently from multi-button clusters.

jQuery
$( ".tool-group button:only-child" ).css({
  borderRadius: "999px",
  padding: "8px 16px"
});
Try It Yourself

How It Works

Scoping to .tool-group limits which parents are checked — each group with one button gets the pill style.

🚀 Common Use Cases

  • Single-action cards — style button:only-child as a full-width CTA.
  • Mini menus — detect li:only-child for one-item dropdowns.
  • Empty states — emphasize the lone paragraph in a message panel.
  • Form rows — widen inputs that are the only child in a field wrapper.
  • Validation — count parents where a required child appears alone.
  • CSS parity — reuse :only-child in stylesheets and jQuery.

🧠 How jQuery Evaluates :only-child

1

Parse selector

Combine type selector with :only-child — e.g. div button:only-child.

Query
2

Inspect parent

Count element children of the parent — text nodes are ignored.

CSS
3

Require exactly one

Keep the element only if it is that sole element child and matches the type prefix.

Filter
4

Return collection

Chain .text(), .css(), or other jQuery methods.

📝 Notes

  • Available since jQuery 1.1.4 — standard CSS pseudo-class.
  • Official rule: if the parent has other child elements, nothing matches.
  • Text and comment nodes do not count as element siblings.
  • Can match multiple elements — one per qualifying parent.
  • Works in native querySelectorAll — no jQuery extension caveat.
  • Not the same as :only-of-type — tag-scoped vs all element siblings.

Browser Support

The :only-child pseudo-class is standard CSS and works in jQuery 1.1.4+. All modern browsers support document.querySelectorAll("li:only-child"). Same syntax in stylesheets and jQuery selectors.

CSS · jQuery 1.1.4+

jQuery :only-child Selector

Universal browser support. Native: querySelectorAll("button:only-child").

100% Standard CSS
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 · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
:only-child Universal

Bottom line: Use button:only-child or li:only-child — scope to parents; prefer over bare :only-child.

Conclusion

The :only-child selector matches every element that is the only element child of its parent — the official div button:only-child demo labels lone buttons “Alone” with a blue border.

Remember the sibling rule from official docs, distinguish :only-of-type, and reuse the same selector in CSS when possible.

💡 Best Practices

✅ Do

  • Use .toolbar button:only-child for scoped queries
  • Choose :only-child when no element siblings should exist
  • Pair with :only-of-type docs when tag type matters
  • Remember text nodes do not block a match
  • Reuse the same selector in CSS and jQuery

❌ Don’t

  • Confuse :only-child with :only-of-type without reading docs
  • Expect a match when another element child exists
  • Use bare $(":only-child") on large pages
  • Assume a lone tag among other tags matches :only-child
  • Replace structural checks with .length === 1 without reason

Key Takeaways

Knowledge Unlocked

Five things to remember about :only-child

Only element child of its parent.

5
Core concepts
0 02

Sibling rule

Elements only

Rule
1st+last 03

Equivalent

Both at once

Tip
type 04

≠ of-type

Tag scope

Compare
demo 05

Official

Alone btn

Demo

❓ Frequently Asked Questions

:only-child selects every element that is the only element child of its parent — no sibling elements before or after it. $("div button:only-child") matches buttons in parents that contain just that one button element. Available since jQuery 1.1.4.
Nothing matches. Official jQuery docs state that if the parent has other child elements, :only-child matches nothing for that parent. A button beside another button, span, or div sibling will not match.
An element that is the only element child is simultaneously :first-child and :last-child. In practice, p:only-child matches the same nodes as p:first-child:last-child when no other element siblings exist.
:only-child requires no element siblings at all. :only-of-type allows siblings of other tag names — a lone span among div siblings can match span:only-of-type but not span:only-child.
Yes. :only-child is standard CSS and works in native querySelectorAll — e.g. document.querySelectorAll("li:only-child"). jQuery has supported it since 1.1.4.
No. CSS structural pseudo-classes consider element siblings only. Text nodes like "Sibling!" in the official demo do not prevent a lone button from matching :only-child.
Did you know?

The official jQuery :only-child demo floats purple div boxes and labels lone buttons “Alone.” Buttons sitting next to other buttons — even with “Sibling!” text between them — never match because element siblings disqualify the parent.

Continue to :nth-child() Selector

Learn fixed indexes, even/odd stripes, and An+B patterns for child-position matching.

:nth-child() tutorial →

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.

6 people found this page helpful