css nth-child用法

本文详细介绍了CSS中用于选择HTML元素的各种选择器,包括:first-child、:last-child、:not(:last-child)、:nth-of-type(odd/even)、:nth-child(n/-n+3/n+3)及:nth-last-child(n)等,帮助读者掌握如何精确控制样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 选择第一个元素 :first-child
 li:first-child{
    color : red;
 }
  1. 选择最后一个元素 :first-child
 li:last-child{
    color : red;
 }
  1. 选择非最后一个元素 :not(:last-child)
 li:not(:last-child){
    color : red;
 }
  1. 选择偶数行元素 :nth-of-type(odd)或:nth-child(2n)
 li::nth-of-type(odd){
    color : red;
 }
  1. 选择奇数行元素 :nth-of-type(even)或:nth-child(2n-1)
 li::nth-of-type(even){
    color : red;
 }
  1. 选择第n个元素 :nth-child(n)
  li:nth-child(1){
    color : red;
 }
  1. 选择第倒数n个元素 :nth–last-child(n)
  li:nth-last-child(1){
    color : red;
 }
  1. 选择前n个元素 :nth-child(-n+3) 【第一个到第三个】
  li :nth-child(-n+3){
    color : red;
 }
  1. 选择从第几个开始 :nth-child(n+3) 【第三个开始到最后】
  li :nth-child(n+3){
    color : red;
 }
  1. 选择某一范围的元素 ::nth-child(-n+3):nth-child(n+3) 【第三个到倒数第三个之间的元素】
  li:nth-child(-n+3):nth-child(n+3){
    color : red;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值