gpt4 book ai didi

html - 如何在 css3 选择器中交替显示两列的颜色?

转载 作者:技术小花猫 更新时间:2023-10-29 12:05:05 25 4
gpt4 key购买 nike

如果我有这段代码

#list>div {
font-size: 18px;
float: left;
margin: 0 10px 10px 0;
width: 150px;
}

#list>div:nth-child(2n+1) {
clear: left;
}

#list>div:nth-child(odd) {
background-color: red;
}

#list>div:nth-child(even) {
background-color: blue;
}
<div id="list">
<div class="list-item">A</div>
<div class="list-item">B</div>
<div class="list-item">C</div>
<div class="list-item">D</div>
<div class="list-item">E</div>
<div class="list-item">F</div>
<div class="list-item">G</div>
<div class="list-item">H</div>
<div class="list-item">I</div>
<div class="list-item">J</div>
</div>

这样显示

A B
C D
E F
G H
I J

这很好,但是,我希望背景颜色像

red blue
blue red
red blue
blue red
red blue

但是上面的代码使每一列的颜色相同。有没有纯 css3 的方法可以做到这一点?

最佳答案

#list>div {
font-size: 18px;
float: left;
margin: 0 10px 10px 0;
width: 150px;
}

#list>div:nth-child(2n+1) {
clear: left;
}

#list>div:nth-child(4n+1), #list>div:nth-child(4n) {
background-color: red;
}

#list>div:nth-child(4n+2), #list>div:nth-child(4n+3) {
background-color: blue;
}
<div id="list">
<div class="list-item">A</div>
<div class="list-item">B</div>
<div class="list-item">C</div>
<div class="list-item">D</div>
<div class="list-item">E</div>
<div class="list-item">F</div>
<div class="list-item">G</div>
<div class="list-item">H</div>
<div class="list-item">I</div>
<div class="list-item">J</div>
</div>

关于html - 如何在 css3 选择器中交替显示两列的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42679108/

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