Skip to content
Tutorials Center
My learning

Topics

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.

CSS
li:first-child {
  border-top: 0;
}

Try it

Style the first item

Syntax

CSS
: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-child is two conditions. The element must be a p and 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 gap in flex or grid containers over first/last-child margin tricks when you can.
  • Descendant gotcha. .list :first-child matches 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.