概述

:nth-last-child(an+b) 这个 CSS 伪类匹配文档树中在其之后具有 an+b-1 个兄弟节点的元素,其中 n 为正值或零值。它基本上和 :nth-child 一样,只是它从结尾处反序计数,而不是从开头处。

可以在 :nth-child 中查看关于语法参数更详细的描述。

语法

: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)
表示HTML表的倒数的奇数行:5、10、15等。
:nth-last-child(3n+4)
表示HTML表的倒数的奇数行: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开始,n和n+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 AndroidEdge MobileFirefox 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 YesEdge Mobile Full support 12Firefox 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 NoEdge Mobile 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 57Edge Mobile No support NoFirefox 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.

相关资料

文档标签和贡献者

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