設定每個

父元素下唯一子元素的樣式


使用 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 年 6 月 22 日

118 次瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.