CSS参考手册
选择 | Selectors
:nth-last-of-type
该: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 |
选择 | Selectors相关

层叠样式表( Cascading Style Sheets )是一种用来表现 HTML 或 XML 等文件样式的计算机语言。CSS 不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。