CKTIS / CSS
:only-child — an element without siblings
The :only-child pseudo-class selects an element without element siblings. Text and comments do not affect matching, but hidden elements do.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
:only-childThe :only-child pseudo-class matches an element without element siblings. It takes no arguments. The selector p:only-child selects a paragraph only when it has no element siblings, including siblings of a different type.
Text and comments do not prevent a match. A hidden sibling element still prevents it. Unlike :only-of-type, :only-child considers all element types, not just the candidate’s type.
Matching is equivalent to :first-child:last-child, but specificity is lower: (0,1,0) rather than (0,2,0) for these pseudo-classes alone. In the example, only #solo matches; the hidden span disqualifies #paired.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>:only-child example</title>
<style>
.box > p:only-child { border: 2px solid blue; }
</style>
<div class="box">
Text before the element.
<!-- Comments do not affect matching. -->
<p id="solo">A</p>
</div>
<div class="box">
<p id="paired">B</p>
<span hidden>C</span>
</div>
<pre id="result"></pre>
<script>
const matches = document.querySelectorAll('.box > p:only-child');
document.getElementById('result').textContent =
Array.from(matches, element => element.id).join(', ');
</script>
</html>Result
solo
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.