【css】css奇数、偶数、指定数选择器:
warning:
这篇文章距离上次修改已过190天,其中的内容可能已经有所变动。
在CSS中,我们可以使用:nth-child(an+b)
伪类选择器来选择元素,其中a和b是任意给定的整数,n是一个自然数,它是子元素的位置。
- 选择奇数元素
我们可以使用:nth-child(odd)
选择器来选择奇数元素。
li:nth-child(odd) {
color: red;
}
- 选择偶数元素
我们可以使用:nth-child(even)
选择器来选择偶数元素。
li:nth-child(even) {
color: blue;
}
- 选择指定的元素
我们可以使用:nth-child(an+b)
选择器来选择位置为an+b的元素。
li:nth-child(3n+1) {
color: green;
}
在上述例子中,我们选择了位置为3n+1的元素,即1、4、7、10等元素。
注意:在:nth-child(an+b)
选择器中,a和b必须是整数,n是一个自然数,它是子元素的位置。如果a=0,则选择所有位置为bn+b的元素。如果a<0,则选择所有位置为an+b的元素,其中an+b在给定范围内。
评论已关闭