此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

:first-child

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

{"* "}Some parts of this feature may have varying levels of support.

:first-child CSS 伪类表示在一组兄弟元素中的第一个元素。

尝试一下

语法

示例

基础示例

HTML

CSS

结果

样式化列表

HTML

html
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>
    Item 3
    <ul>
      <li>Item 3.1</li>
      <li>Item 3.2</li>
      <li>Item 3.3</li>
    </ul>
  </li>
</ul>

CSS

css
ul li {
  color: blue;
}

ul li:first-child {
  color: red;
  font-weight: bold;
}

结果

规范

Specification
Selectors Level 4
# first-child-pseudo

浏览器兼容性

参见