繁体   English   中英

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

[英]How to alternate colors in two columns in css3 selector?

如果我有这个代码

 #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> 

看一下这个:

 #list>div { font-size: 18px; float: left; margin: 0 10px 10px 0; width: 150px; background-color: red; } #list>div:nth-child(4n+1) { background-color: blue; } #list>div:nth-child(4n) { background-color: blue; } #list>div:nth-child(2n+1) { clear:left } 
 <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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM