今天说说九个CSS3结构性伪类选择器
一、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; } |