使用 CSS 對無子代

元素設定樣式


要使用 CSS 對無子代的每個 <p> 元素設定樣式,請使用:empty 選擇器。你可以嘗試執行以下程式碼,以實現: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

815 瀏覽量

開啟你的 職業生涯

完成課程,取得認證

開始
廣告