CSS3结构性伪类选择器九种写法
程序员文章站
2023-10-21 22:28:01
今天说说九个CSS3结构性伪类选择器... 12-04-18...
一、x:nth-child(n)
example source code : li:nth-child(3) { color: red; } |
: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)
example source code : li:nth-last-child(2) { color: red; } |
三、x:nth-of-type(n)
example source code: ul:nth-of-type(3) { border: 1px solid black; } |
四、x:nth-last-of-type(n)
example source code : ul:nth-last-of-type(3) { border: 1px solid black; } |
五、x:first-child
example source code: ul li:first-child { border-top: none; } |
六、x:last-child
example source code : ul > li:last-child { color: green; } |
留意ie8支持:first-child,但不支持:last-child。
七、x:only-child
example source code: div p:only-child { color: red; } |
八、x:only-of-type
example source code: li:only-of-type { font-weight: bold; } |
九、x:first-of-type
example source code : ul:first-of-type{ font-weight: bold; } |
下一篇: CSS3中Color的一些特性介绍