:nth-last-child() 这个CSS 伪类 从兄弟节点中从后往前匹配处于某些位置的元素

/* 在所有兄弟节点中,从后往前
   选择所有4的倍数的节点 */
:nth-last-child(4n) {
  color: lime;
}

注意: 这个伪类和 :nth-child 基本一致, 但它是从结尾计数, 而不是从开始计数.

语法

nth-last-child伪类接受一个参数, 用来作为一个模式,从后往前匹配元素.

Keyword values

odd
代表一些元素,它们在所在的兄弟节点中,从后往前计算的数字位置是奇数,比如: 1, 3, 5等.
even
代表一些元素,它们在所在的兄弟节点中,从后往前计算的数字位置是偶数,比如: 2, 4, 6等.

Functional notation

<An+B>
代表一些元素,它们在所在兄弟节点中的数字位置满足模式 An+B, n是0或者任意的正整数. 从结尾开始计算的第一个元素的索引值是1. AB 必须都是 <integer>.

Formal syntax

:nth-last-child( <nth> [ of <complex-selector-list> ]? )

where
<nth> = <an-plus-b> | even | odd
<complex-selector-list> = <complex-selector>#

where
<complex-selector> = <compound-selector> [ <combinator>? <compound-selector> ]*

where
<compound-selector> = [ <type-selector>? <subclass-selector>* [ <pseudo-element-selector> <pseudo-class-selector>* ]* ]!
<combinator> = '>' | '+' | '~' | [ '||' ]

where
<type-selector> = <wq-name> | <ns-prefix>? '*'
<subclass-selector> = <id-selector> | <class-selector> | <attribute-selector> | <pseudo-class-selector>
<pseudo-element-selector> = ':' <pseudo-class-selector>
<pseudo-class-selector> = ':' <ident-token> | ':' <function-token> <any-value> ')'

where
<wq-name> = <ns-prefix>? <ident-token>
<ns-prefix> = [ <ident-token> | '*' ]? |
<id-selector> = <hash-token>
<class-selector> = '.' <ident-token>
<attribute-selector> = '[' <wq-name> ']' | '[' <wq-name> <attr-matcher> [ <string-token> | <ident-token> ] <attr-modifier>? ']'

where
<attr-matcher> = [ '~' | | | '^' | '$' | '*' ]? '='
<attr-modifier> = i | s

示例

选择器示例

tr:nth-last-child(odd) or tr:nth-last-child(2n+1)
表示HTML表的倒数的奇数行:1、3、5等。
tr:nth-last-child(even) or tr:nth-last-child(2n)
表示HTML表的倒数的偶数行:2、4、6等。
:nth-last-child(7)
表示倒数第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) or p:nth-last-child(n+1)
表示一组兄弟节点中的每个<p>元素。这与一个简单的p选择器相同。(由于n从0开始,而最后一个元素从1开始,nn+1都会选择相同的元素。)
p:nth-last-child(1) or p:nth-last-child(0n+1)
表示所有处于兄弟节点中倒数第一位的元素<p>。这与:last-child选择器相同。

Table example

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

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;
}

结果

Quantity query

数量查询样式元素取决于它们的数量。在本例中,当给定列表中至少有三个列表项时,列表项变为红色。这是通过组合nth-last-child通用兄弟选择器.的功能来实现的

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

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

Result

规范

Specification Status Comment
Selectors Level 4
:nth-last-child
Working Draft No change.
Selectors Level 3
:nth-last-child
Recommendation Initial definition.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
:nth-last-child()Chrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 9Opera Full support 9Safari Full support 3.2WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3.2Samsung Internet Android Full support Yes
of <selector> syntaxChrome No support No
Notes
No support No
Notes
Notes See bug 304163.
Edge No support NoFirefox No support No
Notes
No support No
Notes
Notes See bug 854148.
IE No support NoOpera No support NoSafari Full support 9WebView Android No support NoChrome Android No support NoFirefox Android No support No
Notes
No support No
Notes
Notes See bug 854148.
Opera Android No support NoSafari iOS Full support 9Samsung Internet Android No support No
Matches elements with no parentChrome Full support 57Edge No support NoFirefox Full support 52IE No support NoOpera Full support 44Safari No support NoWebView Android Full support 57Chrome Android Full support 57Firefox Android Full support 52Opera Android Full support 43Safari iOS No support NoSamsung Internet Android Full support 7.0

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.

相关资料

文档标签和贡献者

此页面的贡献者: happyWang, Ritr, mdnwebdocs-bot, PoppinL, hutuxu
最后编辑者: happyWang,