建站知识
Service Support
我们在mf1288前面的文章中,陆续为大家讲了多种CSS选择器。今天说说九个mf1288结构性伪类选择器。
一、X:nth-child(n)
接下来的几个伪类选择器使用上非常类似,功能也比较接近。
:nth-child(n),用于匹配索引值为n的子元素。索引值从1开始。
X:nth-child()用法实际上有三种变化,demo的用法是最简单的,X:nth-child()更强大的用处在于奇偶匹配,明河不展开讲,有兴趣的请看《Understanding :nth-child Pseudo-class Expressions》,《CSS3 :nth-child()伪类选择器》
二、X:nth-last-child(n)
:nth-child(n),是从第一个开始算索引,而X:nth-last-child(n)是从最后一个开始算索引。
三、X:nth-of-type(n)
nth-of-type与nth-child的效果是惊人的相似,想要更多的了解nth-of-type请看《Alternative for :nth-of-type() and :nth-child()》,:nth-of-type(N)
四、X:nth-last-of-type(n)
:nth-last-child效果相似。
五、X:first-child
匹配子集的第一个元素。IE7就可以支持了,这个伪类还是非常有用的。
六、X:last-child
与:first-child效果相反
留意IE8支持:first-child,但不支持:last-child。
七、X:only-child
这个伪类一般用的比较少,比如上述代码匹配的是div下的有且仅有一个的p,也就是说,如果div内有多个p,将不匹配。
八、X:only-of-type
与:only-child类似。
九、X:first-of-type
等价于:nth-of-type(1),匹配集合元素中的第一个元素。