第 n 个子级 CSS 选择器
我有九组配色方案,我想将它们应用于一系列 div。使用 :nth-child(1), :nth-child(2)...
适用于前九个,但我希望该序列在那之后重复,但我不能我的头围绕着 (3n+2) 符号......我想我明白了,但我似乎无法哄它做我想做的事。
这是可能的,还是我应该在写出它们时对每个 div 应用一个类?
I have nine sets of color schemes that I want to apply to a sequence of divs. Using :nth-child(1), :nth-child(2)...
works for the first nine, but I'd like the sequence to then repeat after that, and I can't wrap my head around the (3n+2) notation... I think I get it, but I can't seem to coax it into doing what I want.
Is this possible, or should I just apply a class to each div as I write them out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的意思是需要对每九个连续元素应用不同的规则,则必须使用这九个选择器:
If you mean you need to apply different rules to every nine consecutive elements, you have to use these nine selectors:
首先是一些花絮:
1
的索引进行匹配(即nth-child(1)
是第一个孩子,而不是第二个)n
是迭代器值An + B
表示法中的n
,从0
开始并向上计数An + B
code> 将是一个匹配的索引(我将其称为i
)阅读规范以获取更多信息
如果您有一组要匹配的元素,您应该将它们写出来:
示例:
在本例中,您希望将
n
与特定索引匹配如果我们求解
An + B = i< /code> 使用
n = 0
,i = 1
我们可以获得 B 的值:然后我们可以在第二次替换中使用该值,使用
n = 1
、i = 10
:所以我们现在有
9n + 1
选择器来匹配1,10,19,28,etc
您可以对每个不同的选择进行冲洗和重复,但很快您应该意识到重复发生在每个
A
元素上,偏移量是B
元素。nth-child
选择器是一个很好的现实例子,说明高中代数实际上很有用First a few tidbits:
1
-based indices for matching (i.e.nth-child(1)
is the first child, not the second)n
in theAn + B
notation is the iterator valuen
starts at0
and counts upAn + B
will be a matched index (I'll call iti
)read the spec for more info
If you have a set of elements you want to match, you ought to write them out:
Example:
In this case you want to match
n
to specific indicesIf we solve for
An + B = i
usingn = 0
,i = 1
we can get the value of B:We can then use this value in a second substitution using
n = 1
,i = 10
:So we now have
9n + 1
for a selector to match1,10,19,28,etc
You can rinse and repeat for each different selection, but pretty soon you ought to realize that the repetition happens every
A
elements, and the offset isB
elements.The
nth-child
selector is a great real-world example of where high-school algebra is actually useful