:nth-last-of-type (Selectors) – CSS 中文开发手册
:nth-last-of-type (Selectors) - CSS 中文开发手册
该:nth-last-of-type(an+b)CSS伪类匹配的是在它之后有an+b-1个同类型同胞元素的元素,其中n的是正数或零。它本质上与:nth-of-type是一样的,除了它从后向前计数项目,而不是从前往后。
/* 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的文档寻求更全面的描述。
语法
:nth-last-of-type( <nth> )where <nth> = <an-plus-b> | even | odd
实例
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; }
结果
规范
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 |