css 中 nth-child、first-child、last-child 的使用(选中第一个,第几个,第几个到第几个,最后一个等)

可以配合 li 标签使用,选择一列中的哪些标签。

 1.first-child 选择列表中的第一个标签

li:first-child{color:red}

 2. last-child 选择列表中的最后一个标签

li:last-child{color:pink}

 3.nth-child(n)

这里的n为数字,表示选择列表中的第n个标签

例如选择第三个标签

li:nth-child(3){color:pink}

 4.nth-child(2n) 

选择列表中的偶数,选中 2、4、6…… 个标签。

li:nth-child(2n){color:pink}

 5.同理.nth-child(2n-1)就表示选中了奇数位标签 

 li:nth-child(2n-1){color:pink}

 6.选择从第4个到最后一个标签,这个4可以提换成你需要的数字

li:nth-child(n+4){color:pink}

 7. 选择从第一个到第四个 这里的数字4也是可以根据你的需要替换的。

li:nth-child(-n+4){color:pink}

 8.nth-last-child(3) 表示最后三个标签

li:nth-last-child(3){color:pink}

  9.nth-last-child(3n) 表示3的倍数3.6.9……

li:nth-last-child(3n){color:pink}

 10.nth-last-child(3n-1) 表示2.5.8…… 可以用这个设置等差数列的样式

li:nth-last-child(3n-1){color:pink}

希望有帮到你。

posted @   浅岛连云  阅读(633)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2023-08-21 公司信息
2023-08-21 内容tag 随机文章 相关内容(
2023-08-21 全站tag列表 文章归档 友情链接
2023-08-21 获取单页内容
2023-08-21 首页获取文章 、产品列表信息
2023-08-21 轮播图
2023-08-21 导航代码
点击右上角即可分享
微信分享提示