CSS 子选择器

cssweb developmentfront end technology更新于 2025/5/14 2:20:17

如果要选择指定元素的所有直属子元素,请使用子选择器。

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>

输出


相关文章


有用资源