:last-child

**:last-child** CSS 伪类 表示一组兄弟元素中的最后一个元素。

试试看

语法

css
:last-child {
  /* ... */
}

示例

基本示例

HTML

html
<div>
  <p>This text isn't selected.</p>
  <p>This text is selected!</p>
</div>

<div>
  <p>This text isn't selected.</p>
  <h2>This text isn't selected: it's not a `p`.</h2>
</div>

CSS

css
p:last-child {
  color: lime;
  background-color: black;
  padding: 5px;
}

结果

设置列表的样式

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:last-child {
  border: 1px solid red;
  color: red;
}

结果

规范

规范
选择器级别 4
# the-last-child-pseudo

浏览器兼容性

BCD 表格仅在浏览器中加载

另请参阅