CSS/Selectors/pseudo-classes/:only-of-type

From W3C Wiki
< CSS‎ | Selectors

Pseudo-class :only-of-type

The :only-of-type pseudo-class represents an element that has a parent element and whose parent element has no other element children with the same expanded element name. Same as :first-of-type:last-of-type or :nth-of-type(1):nth-last-of-type(1), but with a lower specificity.

Syntax

selector:only-of-type{ properties }


Example

[style.css]

 h1:only-of-type{
   background-color: yellow;
 }


[index.html]

<body>
  <h1>Only-child example</h1>
  <p>This is a paragraph.</p>
  <p>This is a paragraph.</p>
</body>

CSS defines the :only-of-type pseudo-class selector in 6.6.5.11. :only-of-type pseudo-class.