css 伪类选择器:nth-child() 应用实例

文章摘要:css伪类选择器中一个神奇的玩意,下面我将用几个典型的实例来给大家讲解:nth-child的实际用途:nth-child(2),:nth-child(n+4),:nth-child(-n+4),:nth-child(2n),:nth-child(2n
css伪类选择器中一个神奇的玩意,可以轻松选取你想要的标签并给与修改添加样式,是不是很给力,它就是“:nth-child”。
 
下面我将用几个典型的实例来给大家讲解:nth-child的实际用途:
 
Tips:还用低版本的IE浏览器的哥们请绕过!
 
:nth-child(2)选取第几个标签,“2可以是你想要的数字”
.demo li:nth-child(2){background:#090}
 
:nth-child(n+4)选取大于等于4标签,“n”表示从整数,下同
.demo li:nth-child(n+4){background:#090}
 
:nth-child(-n+4)选取小于等于4标签
.demo li:nth-child(-n+4){background:#090}
 
:nth-child(2n)选取偶数标签,2n也可以是even
.demo li:nth-child(2n){background:#090}
 
:nth-child(2n-1)选取奇数标签,2n-1可以是odd
.demo li:nth-child(2n-1){background:#090}
 
:nth-child(3n+1)自定义选取标签,3n+1表示“隔二取一”
.demo li:nth-child(3n+1){background:#090}
 
:first-child选取第一个标签
.demo li:first-child{background:#090}
 
:last-child选取最后一个标签
.demo li:last-child{background:#090}
  • 版权声明:本站原创文章,于2020-07-14 11:20,由 发表。
  • 转载请注明出处