CSS中:nth-child的用法

做前端必定要记住: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个

© 版权声明

相关文章

1 条评论

  • 头像
    早餐就吃全麦吐司 投稿者

    CSS中的用法

    无记录
    回复