CSS :empty 选择器的作用


使用 CSS :empty 选择器,可以设置样式给任何没有子元素的 <p> 元素。您可以尝试运行以下代码以实现 :empty 选择器:

示例

实时演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         p.demo {
            width: 300px;
            height: 20px;
            background: gray;
          }
          p:empty {
             width: 150px;
             height: 20px;
             background: orange;
         }
      </style>
   </head>
   <body>
      <p class = "demo">This is demo text. Below is empty text.</p>
      <p></p>
   </body>
</html>

更新于: 2020 年 6 月 30 日

154 次浏览

开启你的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.