I'm trying to add background color to alternating set of 'n' number of elements. Say for example, the first 'n' number of elements will be red and the next 'n' number of elements will be black and so on.. repeating the loop of red and black.
Please ignore the number 4 in the grid-template-columns: repeat(4, auto) It is supposed to be dynamic
I can't change the HTML structure nor can I do it using <table>
Can this be achieved only using css? If not a JS answer would also be appreciated
.table {
margin-bottom: 20rem;
border: grey 0.5px solid;
display: grid;
width: 100%;
border-collapse: collapse;
}
.table-cell {
display: flex;
align-items: center;
padding: 1em;
border: grey 1px solid;
}
.table-cell:nth-child(4n) {
background-color: #e0e0e0;
}
.table-row {
grid-template-columns: repeat(4, auto);
display: grid;
}
<div class="table">
<div class="table-row">
<div class="table-cell">content</div>
<div class="table-cell">content</div>
<div class="table-cell">content</div>
<div class="table-cell">content</div>
<div class="table-cell">content</div>
<div class="table-cell">content</div>
<div class="table-cell">content</div>
<div class="table-cell">content</div>
<div class="table-cell">content</div>
<div class="table-cell">content</div>
<div class="table-cell">content</div>
<div class="table-cell">content</div>
</div>
</div>
<tr>?