:nth-child 选择器
:nth-child() 选择器可以指定父元素中一个或多个子元素
兼容性:IE8+
:nth-child(2) 选取第2个标签
/* :nth-child(2)选取第2个标签 */
li:nth-child(2){
background:#ff0000
}
:nth-child(n+4) 选取大于等于4的标签
/* :nth-child(n+4)选取大于等于4的标签 */
li:nth-child(n+4){
background:#ff0000
}
:nth-child(-n+4) 选取小于等于4的标签
/* :nth-child(-n+4)选取小于等于4的标签 */
li:nth-child(-n+4){
background:#ff0000
}
:nth-child(even) 选取偶数标签,even可以写成2n(倍数写法)
/* :nth-child(even)选取偶数标签(2,4,6,8...) */
li:nth-child(even){
background:#ff0000
}
||
/* :nth-child(2n)选取偶数标签,所有2的倍数(2,4,6,8...) */
li:nth-child(2n){
background:#ff0000
}
:nth-child(odd) 选取奇数标签 ,odd可以写成2n-1
/* :nth-child(odd)选取奇数标签(1,3,5,7...) */
li:nth-child(odd){
background:#ff0000
}
||
/* :nth-child(2n-1)选取奇数标签,所有2的倍数减1(2-1=1,4-1=3,6-1=5,8-1=7,...) */
li:nth-child(2n-1){
background:#ff0000
}
:nth-child(3n+1) 倍数分组匹配,3n+1表示“隔二取一”
/* :nth-child(3n+1)从第1个开始每3个一组(1,4,7,10...) */
li:nth-child(3n+1){
background:#ff0000
}
/* 拓展 */
/* :nth-child(3n+5)从第5个开始每3个一组(5,8,11,14...) */
li:nth-child(3n+1){
background:#ff0000
}
:first-child 选取第一个标签
注意:因为不需要参数,结尾没有括号
/* :first-child 选取第一个标签 */
li:first-child{
background:#ff0000
}
:last-child 选取最后一个标签
注意:因为不需要参数,结尾没有括号
/* :last-child 选取最后一个标签 */
li:last-child{
background:#ff0000
}
:nth-last-child(3) 选取倒数第3个标签
/* :nth-last-child(3) 选取倒数第3个标签 */
li:nth-last-child(3){
background:#ff0000
}
只为记录,内容有借鉴,勿喷!