在线文档教程
CSS

:nth-last-of-type

: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; }

结果

规范

SpecificationStatusComment
Selectors Level 4The definition of ':nth-last-of-type' in that specification.Working DraftMatching elements are not required to have a parent.
Selectors Level 3The definition of ':nth-last-of-type' in that specification.RecommendationInitial definition.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support4.0(Yes)3.5 (1.9.1)9.09.53.2

FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support2.1(Yes)1.0 (1.9.1)9.010.03.2