CSS :first-child selector
Complete CSS Selector Reference Manual
Example
Matches
the first child of its parent element
Element
p
:first-child
{
background-color:
yellow
;
}
Try it »
Definition and Usage
:first-child selector matches the first child element of its parent.
Browser support
The numbers in the table indicate the first browser version that supports this property.
| Selector | |||||
|---|---|---|---|---|---|
| :first-child | 4.0 | 7.0 | 3.0 | 3.1 | 9.6 |
Note: :first-child requires a DOCTYPE declaration in IE8 and earlier IE versions
Related Articles
CSS Tutorial: CSS pseudo-classes
More examples
Example
Select each
inside each element and set its style, where the
element is the first child of its parent:
p
:first-child
i
{
background:
yellow
;
}
Try it »
Example
The style selected for the first
li
:first-child
{
background:
yellow
;
}
Try it »
Example
The style selected for the first child of each
-
element:
ul
:first-child
{
background:
yellow
;
}
Try it »