為每

個元素(作為其父級的唯一

個元素)設定樣式


使用 CSS :only-of-type 選擇器為每

個元素(作為其父級的唯一

個元素)設定樣式。

示例

你可以嘗試執行以下程式碼來實現 :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>

更新日期:01-7 月2020

108 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告