only-of-type

CSS :only-of-type Pseudo Class

The :only-of-type selector matches elements that are the only child of its type.

The p:only-of-type matches a paragraph only if it is the only paragraph inside its parent.

The :only-of-type and the :only-child selectors have similarities, except that the :only-child selector selects the element if its parent has no other children of any type.

Version

Version

Selectors Level 3

Selectors Level 4

Syntax

Syntax

:only-of-type {
  css declarations;
}

Example of the :only-of-type selector:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      p:only-of-type {
        background: #1c87c9;
      }
    </style>
  </head>
  <body>
    <h2>:only-of-type selector example</h2>
    <div>
      <p>Lorem ipsum is simply dummy text.</p>
    </div>
    <div>
      <p>Lorem ipsum is simply dummy text.</p>
      <p>Lorem ipsum is simply dummy text.</p>
    </div>
  </body>
</html>


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部