CSS first-of-type Selector :first-of-type

What is CSS :first-of-type Selector?

CSS :first-of-type selector matches first sibling of its type in child elements of an parent element.

Syntax

General syntax of :first-of-type selector,

element:first-of-type {
  property: value;
  property: value;
  ...
}

/* match all first sibling of its type child element */
*:first-of-type {
  property: value;
  property: value;
  ...
}

Example

CSS :first-of-type selector select all first sibling of its type child element.

<!DOCTYPE html>
<html>
<head>
  <title>CSS :first-of-type Selector</title>
  <style>
    div :first-of-type {
      color: #FF0000;
    }
  </style>
</head>
<body>
  <div>
    <span>This is first span</span>
    <span>This is second span</span>
    <ul>
      <li>first item</li>
      <li>second item</li>
    </ul>
    <p>First Paragraph</p>
    <p>Second Paragraph</p> 
  </div>
</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.