CSS3 :nth-last-of-type() 选择器
指定每个p元素匹配同类型中的倒数第2个同级兄弟元素背景色:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
p:nth-last-of-type(2) {
background: #ff0000;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
<p><b>注意:</b> Internet Explorer 8以及更早版本的浏览器不支持:nth-last-of-type() 选择器.</p>
</body>
</html>
定义和用法
:nth-last-of-type(n)选择器匹配同类型中的倒数第n个同级兄弟元素。
n 可以是一个数字,一个关键字,或者一个公式。
提示:请参阅::nth-last-child()选择器。该选择器匹配父元素中的倒数第n个结构子元素
浏览器支持
表格中的数字表示支持该属性的第一个浏览器版本号。
选择器 | |||||
---|---|---|---|---|---|
:nth-last-of-type() | 4.0 | 9.0 | 3.5 | 3.2 | 9.6 |
更多实例
奇数和偶数是可以作为关键字使用用于相匹配的子元素,其索引是奇数或偶数。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
p:nth-last-of-type(odd) {
background: #ff0000;
}
p:nth-last-of-type(even) {
background: #0000ff;
}
</style>
</head>
<body>
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p><b>注意:</b> Internet Explorer 8以及更早版本的浏览器不支持:nth-last-of-type() 选择器.</p>
</body>
</html>
使用公式(an+ b).描述:a代表一个循环的大小,N是一个计数器(从0开始),以及b是偏移量。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
p:nth-last-of-type(3n+0) {
background: #ff0000;
}
</style>
</head>
<body>
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
<p>The fifth paragraph.</p>
<p>The sixth paragraph.</p>
<p>The seventh paragraph.</p>
<p>The eight paragraph.</p>
<p>The ninth paragraph.</p>
<p><b>注意:</b> Internet Explorer 8以及更早版本的浏览器不支持 :nth-last-of-type()选择器.</p>
</body>
</html>