對每個元素新增樣式

元素是其父元素的唯一

元素使用 CSS


使用 CSS :only-of-type 選擇器對每個元素新增樣式,它是其父元素的唯一 <p> 元素。

示例

您可以嘗試執行以下程式碼來實施 :only-of-type 選擇器

動態演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         p:only-of-type {
            background: orange;
            color: white;
         }
      </style>
   </head>
   <body>
      <div>
         <p>This is demo text 1.</p>
      </div>
      <div>
         <p>This is demo text 2.</p>
         <p>This is demo text 3.</p>
         <p>This is demo text 4.</p>
      </div>
   </body>
</html>

最新更新於: 2020 年 7 月 1 日

觀看次數:108

開始你的 職業

透過完成課程獲得認證

入門
廣告