gpt4 book ai didi

css - 第 n 个 :child selector in css 中的 n 是什么

转载 作者:行者123 更新时间:2023-11-28 09:00:20 24 4
gpt4 key购买 nike

我很好奇 css 中的第 n 个子选择器。但是现在对我来说很难理解它的逻辑。下面给出的示例突出显示第一个和最后一个跨度。

<div>
<span>This span is limed!</span>
<span>This span is not. :(</span>
<em>This one is odd. </em>
<span>Sadly, this one is not...</span>
<span>But this one is!</span>
</div>
span:nth-child(2n+1) {
background-color: green;
}

最佳答案

n 代表一个自然数。意思是,[0, 1, 2, 3, 4, 5, ...]

在您的 CSS 中,2n+1 因此表示:2*0+1 (= 1), 2*1+1 (= 3), >2*2+1 (= 5), 2*3+1 (= 7) 等等。

您的代码基本上选择了所有奇数子项。可以写得更短,即:

nth-child(odd)

关于css - 第 n 个 :child selector in css 中的 n 是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19474933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com