CSS Reference ยท Pseudo-classes
The :first-child pseudo-class
Reference for the CSS :first-child pseudo-class: select an element that is the first child of its parent, with syntax, examples, common mistakes and support.
The :first-child pseudo-class matches an element that is the first child of its parent. Only element nodes count; text and comments are ignored. It is equivalent to :nth-child(1).
It is handy for removing a top border or margin from the first item in a list, or for giving the first card a special look, without adding extra classes.
li:first-child { border-top: 0; }
Try it
Syntax
:first-child { /* declarations */ }
No arguments; specificity (0,1,0). The condition is about position among all sibling elements, not among siblings of the same tag.
Syntax and arguments
| Part | Description |
|---|---|
:first-child |
Matches an element with no preceding sibling element. |
| No arguments | The pseudo-class is not functional. |
| Equivalent | :nth-child(1) |
| Types only | Use :first-of-type to count by tag name. |
Usage notes
p:first-childis two conditions. The element must be apand be first. If a heading comes before the paragraph, nothing matches.- Whitespace is harmless. Spaces, line breaks, text and comments do not count as children.
- Spacing patterns. Prefer
gapin flex or grid containers over first/last-child margin tricks when you can. - Descendant gotcha.
.list :first-childmatches the first child of every descendant at every depth, not just the first one of.list. - Pair with
:last-child. Together they handle borders and rounded corners on grouped items. - Dynamic content. Inserting an element before the first one moves the match.
Browser support
Supported in every browser.
Related
:last-childโ last sibling:only-childโ the sole child:first-of-typeโ first of its tag name:nth-child()โ match by position formula- Lesson: Structural pseudo-classes