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

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

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

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

li:first-child{color:red}
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}
li:nth-child(2n-1){color:pink}

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

 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}

希望有帮到你。

 

  • 11
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值