nth-* Firefox 3.5中全新的CSS属性

资讯 驱动中国 72 次阅读 0 条评论
分享 Q

Firefox 3.5支持一些全新的CSS3选取器。在这篇文章中,我们会介绍其中的四个::nth-child,:nth-last-child,:nth-of-type 和 :nth-last-of-type。

这些选取器也被称作伪类,可以为已有的选取器增加风格。我们来看一些实际的例子。

:nth-child

这个伪类允许为一组元素添加风格。最常见的例子就是在表格中隔行高亮数据单元格:

tr:nth-child(even)
{
    background-color: #E8E8E8;
}
实际的例子(需要Firefox 3.5):

Row 1
Row 2
Row 3
Row 4

还可以使用特殊的标记来为超过两组元素添加风格。文档中对规则的描述不太准确,但是在例子中的“3”把元素分为三组,“+1”则是在组中的偏移量。在规范中还有很多例子。

tr:nth-child(3n+1) {  background-color: red; }
tr:nth-child(3n+2) {  background-color: green; }
tr:nth-child(3n+3) {  background-color: blue; }
实际的例子(需要Firefox 3.5):

Row 1
Row 2
Row 3
Row 4
Row 5
Row 6

:nth-last-child

:nth-last-child伪类同:nth-child的工作方式非常相似,不过他是从后向前数子节点:

tr:nth-last-child(3n+3) {  background-color: red; }
tr:nth-last-child(3n+2) {  background-color: green; }
tr:nth-last-child(3n+1) {  background-color: blue; }
例子(需要Firefox 3.5):

Row 1
Row 2
Row 3
Row 4
Row 5
Row 6

:nth-of-type

:nth-of-type伪类使用跟其他伪类类似的语法,但是允许你根据元素类型进行选取。

div:nth-of-type(odd) { border-color: red }
div:nth-of-type(even) { border-color: blue }
例子(需要Firefox 3.5):

I should be red!
I should be blue!
:nth-last-of-type

:nth-last-of-type同:nth-of-type功能类似,不过也是从后向前查子节点。

版权声明:本文由驱动中国整理发布,转载需注明出处与原文链接。如有疑问请联系 editor@qudong.com
本文价值 成为第一个评分的人
登录后为本文打分
网友评论0 条评论
正在回复 的评论取消
评论加载中…
🔐

登录后参与互动

评论、点赞均可赚积分
连续签到、邀请好友也有奖励 🎁

电脑端: 微信扫码登录 微信内: 一键登录

微信扫一扫

打开微信「扫一扫」,分享本文到朋友圈或好友