CSS 选择器nth-child

前端的哥们想必都接触过css中一个神奇的玩意,可以轻松选取你想要的标签并给与修改添加样式,是不是很给力,它就是“:nth-child”。

下面我将用几个典型的实例来给大家讲解:nth-child的实际用途:

还用低版本的IE浏览器的哥们请绕过!支持IE9及以后版本。IE7、IE8支持first-child伪类,不支持last-child。

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

?
1
.demo 01  li:nth-child( 2 ){ background : #090 }

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

?
1
.demo 01  li:nth-child(n+ 4 ){ background : #090 }

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

?
1
.demo 01  li:nth-child(-n+ 4 ){ background : #090 }
:nth-child( odd ) 与 :nth-child( even )

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

?
1
.demo 01  li:nth-child( 2 n){ background : #090 }

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

?
1
.demo 01  li:nth-child( 2 n -1 ){ background : #090 }

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

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

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

?
1
.demo 01  li:last-child{ background : #090 }

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

?
1
.demo 01  li:nth-last-child( 3 ){ background : #090 }<br><br>

常用的选择器列表图 


.input-rungroup-dotsep .irg-fields .ipaddr-lastnum:after {
  content: "";
}
.input-rungroup-dotsep .irg-fields li:last-child :after {
  content: "";
}// IE9及以后支持选择器

.input-rungroup-dotsep .irg-fields li:nth-child(-n+3):after {
  content: ".";
}// IE9及以后支持选择器

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值