CSS first-child Selector (:first-child)

What is CSS :first-child Selector?

CSS :first-child selector matches only first child element of its parent element.

Syntax

General syntax of CSS :first-child selector,

:empty {
  property: value;
  property: value;
  ...
}

div:empty {
  property: value;
  property: value;
  ...
}

Example

CSS :first-child selector select only first <li> element of its <ul> parent element.

<!DOCTYPE html>
<html>
<head>
  <title>CSS :first-child Selector</title>
  <style type="text/css">
    li:first-child {
      background:#ff0000;
    }
  </style>
</head>
<body>
  <li>first item</li>
  <li>second item</li>
  <li>third item</li>
  <li>forth item</li>
  <li>fifth item</li>
</body>
</html>

Run it...   »

Browser Compatibility

  • Google Chrome 2+
  • Mozilla Firefox 3+
  • Internet Explorer 9+
  • Opera 9.5+
  • Safari 4+

Note: Here details of browser compatibility with version number may be this is bug and not supported. But recommended to always use latest Web browser.