Skip to: content, navigation

by Tommy Olsson and Paul O’Brien

:first-child (CSS selector)

Browser support full matrix
IE7+ FF1+ Saf1.3+ Op9.5+
Buggy Buggy Buggy Full
Spec
Version
CSS1

Example

This example selector matches the first list item in an ol or ul element:

li:first-child {
  ⋮ declarations
}

Try it yourself!View all demos

Description

This pseudo-class matches an element only if it’s the first child element of its parent element. For instance, li:first-child matches the first list item in an ol or ul element. It doesn’t match the first child of a list item.1

For example, let’s take the CSS selector mentioned above:

li:first-child {
  ⋮ declarations
}

And let’s apply it to the following markup:

<ul>
  <li>This item matches the selector li:first-child.</li>
  <li>This item does not match that selector.</li>
  <li>Neither does this one.</li>
</ul>

Only the first list item element is matched.

Note that this pseudo-class only applies to elements—it doesn’t apply to anonymous boxes generated for text.

Compatibility

IE5.5None
6.0None
7.0Buggy
Firefox1.0Buggy
1.5Buggy
2.0Buggy
Safari1.3Buggy
2.0Buggy
3.0Buggy
Opera9.2Buggy
9.5Full

In Internet Explorer 7 some SGML constructs, such as comments, are counted as elements.

Internet Explorer 7, Firefox versions up to and including 2, Safari versions up to and including 3, and Opera 9.2 continue to select an element as the first child even when another element is dynamically inserted before it.

Internet Explorer 7 and Firefox select the first SGML construct on the page (usually the doctype), even though it’s not a child of another element.

Opera 9.2 selects the root element, html, even though it isn’t a child of another element.

Footnotes

1 To perform that match, you can use li>*:first-child.

User-contributed notes

There are no comments yet.

Add a note

To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

Related Products

The Principles of Beautiful Web Design

Best Seller!

You don’t need to go to Art School to design great looking web sites!

Book Cover: The Principles of Beautiful Web Design

Download the FREE sample chapters