CSS :only-child 選擇器的作用


使用 CSS :only-child 選擇器來設定其父元素的唯一子元素的每個 <p> 元素的樣式。

示例

你可以嘗試執行以下程式碼來實現 :only-child 選擇器

動態演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         p:only-child {
            background: orange;
         }
      </style>
   </head>
   <body>
      <h1>Heading</h1>
      <div>
         <p>This is a paragraph.</p>
      </div>
      <div>
         <p>This is a paragraph.</p>
         <p>This is a paragraph.</p>
         <p>This is a paragraph.</p>
      </div>
   </body>
</html>

更新時間:2020-07-01

103 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.