CSS中:nth-child的用法

大前端 2020年1月9日 3.01K

做前端一定要记住:nth-child(2)这个选择器,它可以帮你轻松选择你想要的标签并给与修改添加样式

下面通过几个例子说明,相信你一看就明白

li:nth-child(2){background:#090}

:nth-child(2)表示选取第几个标签,”2可以是你想要的数字”

li:nth-child(n+4){background:#090}

:nth-child(n+4)选取大于等于4标签,”n”表示从整数

li:nth-child(-n+4){background:#090}

:nth-child(-n+4)选取小于等于4标签

li:nth-child(2n){background:#090}

:nth-child(2n)选取偶数标签,2n也可以是even

li:nth-child(2n-1){background:#090}

:nth-child(2n-1)选取奇数标签,2n-1可以是odd

li:nth-child(3n+1){background:#090}

:nth-child(3n+1)自定义选取标签,3n+1表示”隔二取一”

li:last-child{background:#090}

:last-child选取最后一个标签

li:nth-last-child(3){background:#090}

:nth-last-child(3)选取倒数第几个标签,3表示选取第3个


关注微信公众号『PHP学习网

第一时间了解最新网络动态
关注博主不迷路~

PHP学习网:站内收集的部分资源来源于网络,若侵犯了您的合法权益,请联系我们删除!
分享到:
赞(0)

文章评论

您需要之后才可以评论
0点赞 0评论 收藏 QQ分享 微博分享

PHP学习网

PHP学习网