CSS :only-of-type 伪类

:only-of-type 选择器匹配作为其类型唯一子元素的元素。

p:only-of-type 仅当它是其父级中的唯一段落时才匹配该段落。

:only-of-type 和 :only-child 选择器有相似之处,除了 :only-child 选择器选择元素,如果它的父元素没有任何类型的其他子元素。

语法

:only-of-type {
  css declarations;
}

:only-of-type 选择器的例子:

<!DOCTYPE html>
<html>
  <head>
    <title>文档的标题</title>
    <style>
      p:only-of-type {
        background: #1c87c9;
      }
    </style>
  </head>
  <body>
    <h2>:only-of-type selector example</h2>
    <div>
      <p>生活终归还得继续。.</p>
    </div>
    <div>
      <p>生活终归还得继续。.</p>
      <p>生活终归还得继续。.</p>
    </div>
  </body>
</html>
日期:2020-06-02 22:14:41 来源:oir作者:oir