:nth-last-of-type

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

:nth-last-of-type() CSS 伪类基于元素在相同类型(标签名)的兄弟元素中相对最后一个元素的位置来匹配元素。

尝试一下

语法

nth-last-of-type 伪类通过单个参数来指定元素的匹配模式,从元素列表的末尾开始计数。

请参见 :nth-last-child 以获取更详细的语法说明。

css
:nth-last-of-type(<an-plus-b> | even | odd) {
  /* ... */
}

示例

HTML

html
<div>
  <span>这是一个 span。</span>
  <span>这是另一个 span。</span>
  <em>这是起强调作用的。</em>
  <span>哇,这个 span 元素变成了绿色!!!</span>
  <del>这里被删除了。</del>
  <span>这是最后一个 span。</span>
</div>

CSS

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

结果

规范

Specification
Selectors Level 4
# nth-last-of-type-pseudo

浏览器兼容性

BCD tables only load in the browser

参见