:only-child selects every element that is the only element child of its parent — no sibling elements at all. Official docs: if the parent has other child elements, nothing is matched. Available since jQuery 1.1.4. Standard CSS.
Remember
$(":only-child")
$("div button:only-child") → button alone in its div
$("li:only-child") → sole li in its list
<div><button></button></div> → matches
<div><button></button><button></button></div> → no match
<div>Sibling! <button></button></div> → matches (text ignored)
Pick a type, then keep only those that sit alone under their parent.
Choose the element type (button, li, and so on).
Add :only-child — or combine as div button:only-child.
Confirm the parent has no other element children.
Ignore text nodes; they do not block a match.
Piece
Required?
Role
Type / context
Usually
Limits which tags are tested
:only-child
Yes
Requires zero element siblings
Parent element children
—
Must be exactly one for a match
Pseudocode
Pseudocode
candidates = find(baseSelector)
result = empty
for each el in candidates:
siblings = element children of el.parent
if siblings.length == 1 and siblings[0] == el:
keep el
return jQuery(result)
Reference
Cheat Sheet
Keep these facts handy while matching lone children.
Topic
Detail
Syntax
$(":only-child") / $("sel:only-child")
Rule
Parent has exactly one element child
Text nodes
Ignored — do not count as siblings
vs :only-of-type
All tags vs same tag only
vs first + last
Same as :first-child:last-child
CSS?
Yes — native querySelectorAll
Since
jQuery 1.1.4
Try it
Live Preview
Run selectors on the official-style demo: five divs with buttons and optional text.
Official pattern: lone buttons match; paired buttons do not. Text “Sibling!” is ignored.
Sibling!
Sibling! Sibling!
Alone
A
B
Result
$("div button:only-child")
matched: 3
hits: box 2 → Btn; box 3 → Btn; box 5 → Btn
/* element siblings block; text does not */
Code
jQuery Examples
Short, copy-ready snippets. Open a browser console with jQuery loaded, or use Try It Yourself for a live editor.
Example 1 — Official Demo: div button:only-child
Find buttons that are the only element child of their div (jQuery API example).
only-child: Alone
first+last: Alone
same size: true
How It Works
Single-item list. One li is both first and last.
Two-item list. Neither li is only-child.
Equivalent filters. Counts match for the demo markup.
Readability. Prefer :only-child when that intent is clearer.
Edge Cases & Pitfalls
Check these before relying on :only-child in real projects.
siblings
Any element sibling blocks
A nearby span, p, or second button means zero matches for that parent.
text
Text does not count
Whitespace and labels like “Sibling!” are text nodes — they do not prevent a match.
compare
Not the same as :only-of-type
Need “only this tag among mixed siblings”? Use :only-of-type.
scope
Many parents, many matches
Document-wide, every qualifying parent can contribute one match — not just a single result.
Compatibility
Browser Support
:only-child is standard CSS, available in jQuery since 1.1.4. It works with native querySelectorAll in modern browsers.
✓ jQuery 1.1.4+ · CSS standard
jQuery :only-child
Works in all modern browsers with jQuery or native CSS. Same meaning in stylesheets and selectors.
100%modern browsers
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
:only-childCSS + jQuery
Bottom line: Safe to use in jQuery and in plain CSS — prefer it when you need exactly one element child under a parent.
Analysis
Performance Notes
Approach
Cost
Best for
$("div button:only-child")
Native QSA path
Typical jQuery use
document.querySelectorAll(...)
Native CSS
No jQuery needed
Scope then filter
Smaller sets
Large documents
Because :only-child is CSS, browsers can optimize it. Prefer a tight context (div button:only-child) over bare $(":only-child") on huge pages.
Remember
Key Takeaways
Role: match elements with no element siblings under the same parent.
Text OK: text nodes do not block :only-child.
vs of-type: use :only-of-type when other tags may sit nearby.
Equivalent: same idea as :first-child:last-child.
One line::only-child means “this element is alone under its parent” — text allowed, other elements not.
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 whose parent contains just that one button. Available since jQuery 1.1.4.
Nothing matches for that parent. Official docs: if the parent has other child elements, :only-child matches nothing. A button beside another button, span, or div does not match.
No. Structural pseudo-classes count element siblings only. Text like "Sibling!" in the official demo does not block a lone button from matching :only-child.
A sole element child is both :first-child and :last-child. So 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 of any tag. :only-of-type allows siblings of other tag names — a lone span among divs can match span:only-of-type but not span:only-child.
Yes. :only-child is standard CSS and works with native querySelectorAll — e.g. document.querySelectorAll("li:only-child"). jQuery has supported it since 1.1.4.