CSS 子元素選擇器


如果您想要選擇指定元素的所有直接子元素,請使用子元素選擇器。

div > p

示例

您可以嘗試執行以下程式碼來實現 CSS 子元素選擇器

現場演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         div > p {
            background-color: orange;
         }
      </style>
   </head>
   <body>
      <div>
         <p>Para 1 in the div.</p>
         <!-- This is not a Child -->
         <span><p>Para 2 in the div.</p></span>
      </div>
      <p>Para 3 outside the div.</p>
   </body>
</html>

輸出

更新於: 2020 年 6 月 30 日

319 次檢視

開啟你的職業生涯

透過完成此課程獲得認證

開始學習
廣告
© . All rights reserved.