設定樣式為每個

沒有子元素的元素使用 CSS


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

更新於:30-6 月 -2020

819 檢視次數

開啟您的 事業

透過完成課程獲得認證

開始
廣告