[英]How to select css nth-child(3n+1) selector using class name
I am trying to use nth-child
selector by class name. 我试图通过类名使用nth-child
选择器。 Fiddle 小提琴
HTML : HTML :
<div>
<span class="head">Heading</span>
<div class="span">1</div>
<div class="span">2</div>
<div class="span">3</div>
<div class="span">4</div>
<div class="span">5</div>
<div class="span">6</div>
<div class="span">7</div>
<div class="span">8</div>
<div class="span">9</div>
</div>
CSS : CSS :
.span:nth-child(3n+1){
color: red;
}
why is not selecting first child form the .span
class.? 为什么不从.span
类中选择第一个孩子。
I need some explanation. 我需要一些解释。
When using a formula (an + b). 使用公式(a + b)时。 a represents a cycle size, n is a counter (starts at 0), and b is an offset value. a表示循环大小,n是计数器(从0开始),b是偏移值。
This may be a little tricky but the indexing of elements here start from 2 这可能有点棘手, 但这里的元素索引从2开始
b=2 would means the first element is always choosen. b = 2意味着总是选择第一个元素。 So, 4n+2 would mean starting with 1st element and adding 4 in cycle=1,5,9. 因此,4n + 2意味着从第一个元素开始并在周期中添加4 = 1,5,9。
The value of n starts at 0. n的值从0开始。
Similarly b=3 would mean starting from the 2nd element You can not choose 1st element of the ".span" in the series with nth-child=3n+1. 类似地,b = 3意味着从第2个元素开始你不能在nth-child = 3n + 1的系列中选择“.span”的第1个元素。
1For your series 1,3,6,9... '1' seems to be out of order. 1对于你的系列1,3,6,9 ...'1'似乎出了故障。 If you need to choose first element along with others in your series: 如果您需要在系列中选择第一个元素和其他元素:
Add 加
.span:nth-child(2){
color: red;
}
Hope it explains the use of nth-child. 希望它能解释使用nth-child。 For more reference http://css-tricks.com/how-nth-child-works/ 有关更多参考信息,请访问http://css-tricks.com/how-nth-child-works/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.