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>

更新於: 01-07-2020

103 瀏覽

開啟您的 職業生涯

透過完成課程認證

開始
廣告
© . All rights reserved.