css3 巧用结构性伪类选择器

最近在国外的一个网站上看到的一个关于结构性伪类选择器的用法,以为十分实用,就本身尝试了一下,并把它给记录下来:css

这是最基本的样式:网站

 1 <style type="text/css">
 2     li{
 3         list-style-type: none;
 4         float: left;
 5         width: 60px;
 6         height: 60px;
 7         background-color: #979698;
 8         margin-left: 10px;
 9         text-align: center;
10         line-height: 60px;
11         border-radius: 50%;
13     }
14 </style>

body内的内容:spa

 1 <body>
 2  <ul>
 3      <li>第01个</li>
 4      <li>第02个</li>
 5      <li>第03个</li>
 6      <li>第04个</li>
 7      <li>第05个</li>
 8      <li>第06个</li>
 9      <li>第07个</li>
10      <li>第08个</li>
11      <li>第09个</li>
12      <li>第10个</li>
13      <li>第11个</li>
14  </ul>
15 </body>

首先是最基本的结构性伪类选择器的用法:3d

1     li:nth-child(8){
2         background-color: #298EB2;
4     }

结果展现为:code

 

利用:nth-child(n+6) 至关于:nth-child(6)及以上的li标签元素:blog

1     li:nth-child(n+6){
2         background-color: #298EB2;
4     }

结果展现为:class

 

同理利用:nth-child(-n+6) 至关于:nth-child(6)及如下的li标签元素:原理

1     li:nth-child(-n+6){
2         background-color: #298EB2;
4     }

结果展现为:bfc

 根据以上原理咱们能够来一些进阶的:进阶

好比能够利用 nth-child(n+4):nth-child(-n+8) 达到获取:nth-child(4)及以上和:nth-child(8)及如下的li标签元素:

1     li:nth-child(n+4):nth-child(-n+8){
2         background-color: #298EB2;
3     }

结果展现为:

 

还能够利用 :nth-child(n+2):nth-child(odd):nth-child(-n+8) 获取:nth-child(n+2)到:nth-child(-n+8)之间的单数li标签元素:

1     li:nth-child(n+2):nth-child(odd):nth-child(-n+8){
2         background-color: #298EB2;
3     }

结果展现为:

最后咱们还能够利用:nth-child(3n+1)获取数目为一、四、七、10中的偶数li标签元素:

1     li:nth-child(3n+1):nth-child(even){
2         background-color: #298EB2;
3     }

结果展现为:

相关文章
相关标签/搜索