几个伪选择器

1、:root 指HTML

    :root{background: #D1D1B0;}
    
/*    下面错误代码,无法指定div*/
/*
   div:root{width:1000px;
        height: 1000px;
        background: #eee;}
*/

效果

2、:first-child选择第一个元素

    li:first-child,.ss:first-child{
        background: red;
    }   

    .dd:first-child{
        background: green;
    } 
/*    不是小盒子里第一个元素*/
    .pp:first-child{
        background: yellow;
    } 

效果

3、 :last-child选择最后一个元素

  li:last-child,.ss:last-child{
        background: red;
    }   

    .dd:last-child{
        background: green;
    } 

    .pp:last-child{
        background: yellow;
    } 
     
    i{background: green}

效果

4、:nth-child(n)选择自主选择的元素

     li:nth-child(3),.ss:nth-child(3){
        background: red;
    }   

    .dd:nth-child(3){
        background: green;
    } 
/*   pp 不是小盒子里第3个元素*/
    .pp:nth-child(3){
        background: red;
    }  

效果

5、:nth-child(even)选择机器数字的奇数

(零基索引读取方式,我叫机器数字方便我自己记住)

     li:nth-child(even),.ss:nth-child(even){
        background: blue;
    }   
/*   dd不是机器数字的奇数*/
    .dd:nth-child(even){
        background: green;
    } 
/*    不是小盒子里第一个元素*/
    .pp:nth-child(even){
        background: yellow;
    } 

效果

6、:nth-child(odd)选择机器数字的偶数

    li:nth-child(odd),.ss:nth-child(odd){
        background: red;
    }   

    .dd:nth-child(odd){
        background: green;
    } 

    .pp:nth-child(odd){
        background: blue;
    }
    

效果

7、:only-child选择盒子的唯一元素

效果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值