首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

:nth-last-of-type

:nth-last-of-type(an+b)CSS伪类匹配的是在它之后有an+b-1个同类型同胞元素的元素,其中n的是正数或零。它本质上与:nth-of-type是一样的,除了它从后向前计数项目,而不是从前往后

代码语言:javascript
复制
/* Selects every fourth <div> inside a parent element */
/* Counting backwards from the last one */
div:nth-last-of-type(4n) {
  background-color: lime;
}

请参阅有关:nth-child的文档寻求更全面的描述。

语法

代码语言:javascript
复制
:nth-last-of-type( <nth> )where 
<nth> = <an-plus-b> | even | odd

实例

HTML

代码语言:javascript
复制
<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

代码语言:javascript
复制
span:nth-last-of-type(2) {
  background-color: lime;
}

结果

规范

Specification

Status

Comment

Selectors Level 4The definition of ':nth-last-of-type' in that specification.

Working Draft

Matching elements are not required to have a parent.

Selectors Level 3The definition of ':nth-last-of-type' in that specification.

Recommendation

Initial definition.

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

4.0

(Yes)

3.5 (1.9.1)

9.0

9.5

3.2

Feature

Android

Edge

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

2.1

(Yes)

1.0 (1.9.1)

9.0

10.0

3.2

扫码关注腾讯云开发者

领取腾讯云代金券