CSS only-of-type Selector :only-of-type

What is CSS :only-of-type Selector?

CSS :only-of-type selector matches only one child element of its types and whose are child elements of an parent element.

Syntax

General syntax of :only-of-type selector,

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

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

Example

CSS :only-of-type selector matches only one element of its types and whose are child elements of an parent element.

<!DOCTYPE html>
<html>
<head>
  <title>CSS :only-of-type Selector</title>
  <style type="text/css">
    li:only-of-type {
      background:#ff0000;
    }
  </style>
</head>
<body>
  <ul>
    <li>first item</li>
  </ul>
  <ul>
    <li>first item</li>
    <li>second item</li>
    <li>third item</li>
  </ul>
  <p>
    <span>This is first paragraph</span>
  </p>
</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.