CSS 中 :first-child 伪类的用法

cssweb developmentfront end technology更新于 2025/8/23 23:08:17

使用 :first-child 伪类可以为某个元素的第一个子元素添加特殊样式。

示例

您可以尝试运行以下代码来了解 :first-child 伪类的用法 −

<html>
   <head>
      <style>
         div > p:first-child
         {
            text-indent: 25px;
         }
      </style>
   </head>
   <body>
      <div>
         <p>First paragraph in div. This paragraph will be indented</p>
         <p>Second paragraph in div. This paragraph will not be indented</p>
      </div>
         <p>But it will not match the paragraph in this HTML:</p>
      <div>
         <h3>Heading</h3>
         <p>The first paragraph inside the div. This paragraph will not be effected.</p>
      </div>
   </body>
</html>

相关文章


有用资源