CKTIS / CSS
:first-child — the first element among siblings
The :first-child pseudo-class matches an element that comes first among its element siblings. It does not mean “the first element of this type”: p:first-child will not match a paragraph preceded by a heading.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
:first-child:first-child is a structural CSS pseudo-class written with one colon, without arguments or parentheses. The name first-child without the colon is not the syntax for this pseudo-class. The selector p:first-child matches a p element only when no element sibling precedes it; it is equivalent to p:nth-child(1).
Only elements count when determining the position. Text, whitespace, and comments do not shift that position. This does not mean the first element of a given type: if an h2 precedes a p, p:first-child does not match that paragraph. Use p:first-of-type to select the first p among siblings.
In .group > p:first-child, the > additionally requires the p to be a direct child of an element with class group. Only #selected matches in the example; #blocked follows a heading, and #later follows another paragraph.
CSS Level 2 describes this pseudo-class as the first child of another element. Selectors Level 4 defines it as the first element among its inclusive siblings, so an element without a parent can also match. Do not add a parent requirement to the modern definition.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>:first-child example</title>
<style>
.group > p:first-child { font-weight: bold; }
</style>
<div class="group">
<h2>Heading</h2>
<p id="blocked">Not selected</p>
</div>
<div class="group">
<!-- Comments and whitespace do not affect the position. -->
<p id="selected">Selected</p>
<p id="later">Not selected</p>
</div>
<pre id="result"></pre>
<script>
const matches = document.querySelectorAll('.group > p:first-child');
document.getElementById('result').textContent =
Array.from(matches, element => element.id).join(', ');
</script>
</html>Result
selected
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.