The :first-child pseudo-class matches an element that is the first element child of its parent.
Example: li:first-child styles a list item only when that li is the first child of its parent. It is the same match as :nth-child(1). For “first of this tag,” use :first-of-type.
Baseline · Widely available.
Not deprecated. A long-standing structural pseudo-class from Selectors Level 3 / 4.
Pick the tag.li:first-child only considers list items.
Require first place. The li must also be the parent’s first element child.
Style that item. An orange border marks the match.
Others stay plain. Item 2 and Item 3 are not first children.
Example 2 — When the First Child Is Not a p
p:first-child fails if another element comes first.
CSS
<style>
p:first-child {
color: #16a34a;
font-weight: 700;
}
</style>
<div>
<p>This text is selected!</p>
<p>This text isn't selected.</p>
</div>
<div>
<h2>Not a p — so no p:first-child here.</h2>
<p>This text isn't selected.</p>
</div>
Outer list. Item 1 is the first child of the outer ul.
Inner list. Item 3.1 is the first child of the nested ul.
Same selector.ul li:first-child can match in every list.
Independent parents. First-child is always relative to one parent.
Edge Cases & Pitfalls
Most surprises come from DOM order, not from the selector itself.
order
Another element before the target
If an h2 (or any element) comes first, p:first-child will not match that p.
vs type
Not the same as :first-of-type
Need the first p even after a heading? Use :first-of-type.
text
Whitespace does not block it
In modern CSS, leading text nodes or spaces between tags do not stop :first-child. Earlier elements do.
alias
Same as :nth-child(1)
:first-child and :nth-child(1) select the same elements.
only
Single child also matches
An only child is still the first child. For “exactly one child,” use :only-child.
Compatibility
Browser Support
:first-child is a browser CSS selector. Node.js, Deno, and Bun do not evaluate it.
✓ Baseline · Widely available
:first-child
Supported in all modern browsers for many years (Chrome 1+, Firefox 1+, Safari 3.1+, Edge 12+, IE 7+ with historical quirks in very old engines).
98%Global support
Google Chrome1+
Full support
Mozilla Firefox1+
Full support
Apple Safari3.1+
Full support
Microsoft Edge12+
Full support
Opera9.5+
Full support
Samsung Internet1+
Full support
Internet Explorer7+
Full support*
:first-childWidely supported
Bottom line: *Very old IE had edge-case bugs with comments/anonymous boxes. Safe everywhere for modern sites. It is not a JavaScript runtime API.
Remember
Key Takeaways
Role: match the first element child of a parent.
Form:E:first-child = tag E and first position.
vs :first-of-type: use of-type when another tag can come first.
Alias: same match as :nth-child(1).
One line::first-child styles an element only when it is the first element sibling under its parent.
Frequently Asked Questions
It matches an element that is the first element child of its parent. Other element siblings must not come before it.
Use :first-child alone or with a type, such as li:first-child. That means “an li that is also the first child,” not “the first li after other tags.”
:first-child requires the element to be the first child of any type. :first-of-type matches the first element of that tag among siblings, even if another tag comes first.
Usually another element sits before the target (for example an h2 before a p). Whitespace text nodes do not block :first-child in modern CSS—only earlier element siblings do.
Yes. :first-child matches the same elements as :nth-child(1).
No. It is a CSS selector evaluated by the browser layout engine, not a Node.js or server runtime feature.