: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

瀏覽器相容性

另見