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 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告