伪类选择器
伪类(不存在的类,特殊类)
伪类用来描述一个元素的特殊状态
比如:第一个子元素、被点击的元素、鼠标移入的元素.....
伪类一般情况下都是用 : 开头
:first-child 第一个子元素
:last-child 最后一个子元素
:nth-child()选中第n个子元素
特殊值:
n第n个,n的范围0到正无穷
2n 或者 (even) 表示选中偶数位的元素
2n+1 或者 (odd) 表示选中奇数位的元素
以上这些伪类都是根据所有的子元素进行排序的
<style>
ul > li:last-child{
color: darkmagenta;
}
ul > li:first-child{
color: darkmagenta;
}
ul > li:nth-child(n){
color: darkmagenta;
}
</style>
:first-of-type
:last-of-type
:nth-of-type()
这几个伪类的功能和上述的类似,不通点是他们是同类型元素中进行排序
<style>
ul > li:first-of-type{
color:颜色;
}
ul > li:last-of-type{
color:颜色;
}
ul > li:nth-of-type(n){
color:颜色;
}
</style>
-将符合条件的元素从选择器中去除
<style >
ul > li:not(:nth-of-type(4)) {
color: darkorange;
}
</style>
浙公网安备 33010602011771号