CSS :first-child pseudo-element

Complete CSS Reference Complete CSS Reference

Example

How to style all <p> elements that are the first child of its parent:

p:first-child
{
background-color:yellow;
}

Try it yourself »

Definition and Usage

The :first-child pseudo-element styles the specified selector, only if it is the first child of its parent.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The :first-child selector is supported in all major browsers.

Note: For :first-child to work in IE, a <!DOCTYPE> must be declared.


CSS Version

The :first-child selector was first introduced in:

CSS 2


Related Pages

CSS tutorial: CSS Pseudo classes


Examples

Try it Yourself - Examples


Example

Style the first <i> element of every parent element:

i:first-child
{
background:yellow;
}

Try it yourself »

Example

Style all <i> element of every <p> element, where the <p> element is the first child of its parent:

p:first-child i
{
background:yellow;
}

Try it yourself »

Example

Style the first <li> element of every list:

li:first-child
{
background:yellow;
}

Try it yourself »

Example

Style the first element of every <ul> element:

ul>:first-child
{
background:yellow;
}

Try it yourself »

Complete CSS Reference Complete CSS Reference