css3结构伪类选择器使用小例子

结构伪类选择器

结构伪类选择器
first-child
nth-child(n) 里面可以是数字 可以是even(偶数) odd(奇数) 也可以是式子如 -n+5 表示前五个
last-child

用法与上面基本一样 但是 nth-of-type会先去查看前面指定的元素 再去看目标是排在第几个
nth-of-type(1)

        /* 选择ul里面的第1个li */
        ul li:first-child {
            background: blue;
        }

        /* 选择ul里面的最后1个li */
        ul li:last-child {
            background: purple;
        }


        /* 选择ul里面的第2个li */
        ul li:nth-child(2) {
            background-color: red;
        }

        //注意冒号前面不要打空格

输出:在这里插入图片描述

//偶数
        ul li:nth-child(even) {
            background-color: red;
        }
//奇数
        ul li:nth-child(odd) {
            background-color: blue;
        }

输出
在这里插入图片描述

        /* 用式子表示前五个 */
        ul li:nth-child(-n+5) {
            background-color: red;
        }

输出
在这里插入图片描述

        ul li:nth-of-type(odd) {
            background-color: red;
        }

输出
在这里插入图片描述

记得冒号前不要打空格

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值