What's the best way to select the specific items in a row?
For example, let's make the div.test
which index are odd to be background-color: red;
on odd row, and div.test
which index are even to be background-color: blue;
on even row.
I can hard code it like my example below, but is there any better way of doing this?
The reason I don't use nth-child(odd)
and nth-child(even)
is that the styling of odd items in odd row is different from even items in even row. You will get the idea of running the code snippet...
#ct {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
grid-row-gap: 10px;
grid-column-gap: 10px;
}
.test {
height: 100px;
}
.test:nth-child(1),
.test:nth-child(3),
.test:nth-child(6),
.test:nth-child(8) {
background-color: red;
}
.test:nth-child(2),
.test:nth-child(4),
.test:nth-child(5),
.test:nth-child(7) {
background-color: blue;
}
<div id="ct">
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
</div>