样式化每个独生子

使用 CSS 样式化每个独生子元素


使用 CSS :only-child 选择器来样式化每个是其父元素的独生子的 <p> 元素。

示例

你可以尝试运行以下代码来实现 :only-child 选择器

在线演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         p:only-child {
            background: orange;
         }
      </style>
   </head>
   <body>
      <h1>Heading</h1>
      <div>
         <p>This is a paragraph.</p>
      </div>
      <div>
         <p>This is a paragraph.</p>
         <p>This is a paragraph.</p>
         <p>This is a paragraph.</p>
      </div>
   </body>
</html>

输出

更新日期:2020 年 6 月 22 日

115 次浏览

职业启航

完成课程,获得认证

开始
广告
© . All rights reserved.