:nth-last-child()

Baseline 广泛可用 *

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

* 此特性的某些部分可能存在不同级别的支持。

:nth-last-child() CSS 伪类根据其在同级元素组中的位置匹配元素,从末尾开始计数。

试一试

p {
  font-weight: bold;
}

li:nth-last-child(-n + 3) {
  border: 2px solid orange;
  margin-top: 1px;
}

li:nth-last-child(even) {
  background-color: lightyellow;
}
<p>Eight deadliest wildfires:</p>
<ol reversed>
  <li>Matheson Fire</li>
  <li>Miramichi Fire</li>
  <li>1997 Indonesian fires</li>
  <li>Thumb Fire</li>
  <li>Great Hinckley Fire</li>
  <li>Cloquet Fire</li>
  <li>Kursha-2 Fire</li>
  <li>Peshtigo Fire</li>
</ol>

语法

css
:nth-last-child(<nth> [of <complex-selector-list>]?) {
  /* ... */
}

参数

:nth-last-child() 伪类指定一个参数,该参数表示匹配元素的模式,从末尾开始计数。

关键字值

odd

表示在同级元素系列中,数值位置为奇数(1、3、5 等)的元素,从末尾开始计数。

even

表示在同级元素系列中,数值位置为偶数(2、4、6 等)的元素,从末尾开始计数。

函数式表示法

<An+B>

表示在同级元素系列中,数值位置匹配 An+B 模式的元素,其中 n 为所有正整数或零值,并且

  • A 是一个整数步长,
  • B 是一个整数偏移量,
  • n 是所有非负整数,从 0 开始。

它可以理解为列表中的第 An+B 个元素。从末尾开始计数的第一个元素的索引是 1AB 都必须是 <integer> 值。

of <selector> 语法

通过传递选择器参数,我们可以选择匹配该选择器的倒数第 n 个元素。例如,以下选择器匹配最后三个带 class="important"重要列表项。

css
:nth-last-child(-n + 3 of li.important) {
}

注意:这与将选择器移到函数外部不同,例如

css
li.important:nth-last-child(-n + 3) {
}

此选择器将样式应用于列表项,如果它们也在最后三个子元素中。

示例

选择器示例

tr:nth-last-child(odd)tr:nth-last-child(2n+1)

表示 HTML 表格的奇数行:1、3、5 等,从末尾开始计数。

tr:nth-last-child(even)tr:nth-last-child(2n)

表示 HTML 表格的偶数行:2、4、6 等,从末尾开始计数。

:nth-last-child(7)

表示第七个元素,从末尾开始计数。

:nth-last-child(5n)

表示第 5、10、15 等元素,从末尾开始计数。

:nth-last-child(3n+4)

表示第 4、7、10、13 等元素,从末尾开始计数。

:nth-last-child(-n+3)

表示同级元素组中的最后三个元素。

p:nth-last-child(n)p:nth-last-child(n+1)

表示同级元素组中的每个 <p> 元素。这与简单的 p 选择器相同。(由于 n 从零开始,而最后一个元素从一开始,所以 nn+1 都将选择相同的元素。)

p:nth-last-child(1)p:nth-last-child(0n+1)

表示同级元素组中,从末尾开始计数的每个 <p> 元素。这与 :last-child 选择器相同。

表格示例

HTML

html
<table>
  <tbody>
    <tr>
      <td>First line</td>
    </tr>
    <tr>
      <td>Second line</td>
    </tr>
    <tr>
      <td>Third line</td>
    </tr>
    <tr>
      <td>Fourth line</td>
    </tr>
    <tr>
      <td>Fifth line</td>
    </tr>
  </tbody>
</table>

CSS

css
table {
  border: 1px solid blue;
}

/* Selects the last three elements */
tr:nth-last-child(-n + 3) {
  background-color: pink;
}

/* Selects every element starting from the second to last item */
tr:nth-last-child(n + 2) {
  color: blue;
}

/* Select only the last second element */
tr:nth-last-child(2) {
  font-weight: 600;
}

结果

数量查询

数量查询根据元素的数量来设置样式。在此示例中,当列表中至少有三个列表项时,它们会变为红色。这是通过结合 nth-last-child 伪类和 后续兄弟组合器 的功能来实现的。

HTML

html
<h4>A list of four items (styled):</h4>
<ol>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
</ol>

<h4>A list of two items (unstyled):</h4>
<ol>
  <li>One</li>
  <li>Two</li>
</ol>

CSS

css
/* If there are at least three list items,
   style them all */
li:nth-last-child(n + 3),
li:nth-last-child(3) ~ li {
  color: red;
}

结果

of <selector> 语法示例

在此示例中,有一个无序的姓名列表。某些项目应用了 noted 类,然后用粗底边框突出显示。

HTML

html
<ul>
  <li class="noted">Diego</li>
  <li>Shilpa</li>
  <li class="noted">Caterina</li>
  <li>Jayla</li>
  <li>Tyrone</li>
  <li>Ricardo</li>
  <li class="noted">Gila</li>
  <li>Sienna</li>
  <li>Titilayo</li>
  <li class="noted">Lexi</li>
  <li>Aylin</li>
  <li>Leo</li>
  <li>Leyla</li>
  <li class="noted">Bruce</li>
  <li>Aisha</li>
  <li>Veronica</li>
  <li class="noted">Kyouko</li>
  <li>Shireen</li>
  <li>Tanya</li>
  <li class="noted">Marlene</li>
</ul>

CSS

css
* {
  font-family: sans-serif;
}

ul {
  display: flex;
  flex-wrap: wrap;
  list-style: none;
  font-size: 1.2rem;
  padding-left: 0;
}

li {
  margin: 0.125rem;
  padding: 0.25rem;
  border: 1px solid tomato;
}

.noted {
  border-bottom: 5px solid tomato;
}

在下面的 CSS 中,我们针对标记为 class="noted"奇数列表项。

css
li:nth-last-child(odd of .noted) {
  background-color: tomato;
  border-bottom-color: seagreen;
}

结果

带有 class="noted" 的项目具有粗底边框,并且项目 1、7、14 和 20 具有实心背景,因为它们是带有 class="noted"奇数列表项。

规范

规范
选择器 Level 4
# nth-last-child-pseudo

浏览器兼容性

另见