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
Fundamentals
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.
Concept
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.
Foundation
📝 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" );
Cheat Sheet
⚡ Quick Reference
Parent markup
button:only-child
Why
<div><button></button></div>
Matches
No element siblings
<div><button></button><button></button></div>
No match
Two button siblings
<div><button></button> text </div>
Matches
Text nodes ignored
<div><button></button><span></span></div>
No match
Span is an element sibling
Same as both first and last
:first-child:last-child
Equivalent intersection
Compare
📋 :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
Hands-On
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.
Solo Save button → pill shape
Cut/Copy/Paste group → default square buttons
How It Works
Scoping to .tool-group limits which parents are checked — each group with one button gets the pill style.
Applications
🚀 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.
Important
📝 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.
Compatibility
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.
Bottom line: Use button:only-child or li:only-child — scope to parents; prefer over bare :only-child.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :only-child
Only element child of its parent.
5
Core concepts
101
:only-child
No siblings
API
002
Sibling rule
Elements only
Rule
1st+last03
Equivalent
Both at once
Tip
type04
≠ of-type
Tag scope
Compare
demo05
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.