概述

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

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

语法

:nth-last-of-type( <nth> )

where
<nth> = <an-plus-b> | even | odd

Example

HTML

<div>
  <span>This is a span.</span>
  <span>This is another span.</span>
  <em>This is emphasized.</em>
  <span>Wow, this span gets limed!!!</span>
  <strike>This is struck through.</strike>
  <span>Here is one last span.</span>
</div>

CSS

span:nth-last-of-type(2) {
  background-color: lime;
}

Result

规范

规范 状态 备注
Selectors Level 4
:nth-last-of-type
Working Draft Matching elements are not required to have a parent.
Selectors Level 3
:nth-last-of-type
Recommendation Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
:nth-last-of-type()Chrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 9Opera Full support 9.5Safari Full support 3.2WebView Android Full support 2Chrome Android Full support 18Edge Mobile Full support 12Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3.2Samsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

相关资料

文档标签和贡献者

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