我们有一系列的项目,这些项目排成一行,排在两列display: grid
中.
我们还有5种牌子的 colored颜色 :橙色、蓝色、黄色、绿色、粉色.
行的背景 colored颜色 应按顺序在这些 colored颜色 上迭代,例如:
- 第1行:橙色
- 第2行:蓝色
- 第3行:黄色
- 第4行:绿色
- 第5行:粉色
- 第6行:(返回到5种 colored颜色 中的第一种)橙色
- 第7行:蓝色
- ...等等.
我知道如何使用:nth-child(5n + 1)
遍历每个one项,如下面的代码片段所示.如果我们有<table>
,其中每一对物品都包含在<tr>
中,那么这种方法就可以很好地工作.
但在本例中,项没有分组,我们希望每two个项迭代一次.如何实现逐行上色的效果?
ul {
display: grid;
grid-auto-rows: 50px;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
li:nth-child(5n + 1) {
background: orange;
}
li:nth-child(5n + 2) {
background: blue;
}
li:nth-child(5n + 3) {
background: yellow;
}
li:nth-child(5n + 4) {
background: green;
}
li:nth-child(5n + 5) {
background: pink;
}
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<ul>