CSS 的作用:first-child 选择器


使用 CSS:first-child 选择器可以对作为父元素首个子级元素的每个 <p> 标记设置样式。

示例

可以尝试运行以下代码来实现:first-child 选择器

实时演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         p:first-child {
            background-color: orange;
         }
      </style>
   </head>
   <body>
      <h1>Heading</h1>
      <p>This is demo text.</p>
      <div>
         <p>This is demo text, with the first child of its parent div.</p>
         <p>This is demo text, but not the first child.</p>
      </div>
   </body>
</html>

更新于: 2020 年 6 月 30 日

135 次浏览

开启你的 职业生涯

通过完成课程获取认证

开始学习
广告
© . All rights reserved.