I'm trying to replace a grid of data & interactive components that had been set up in Bootstrap with a CSS Grid-based layout.
I was able to replicate the grid, but am having trouble getting something like table striping working.From what I understand of CSS Grid, since I know the number of columns that will be displayed ahead of time but not the number of rows (dynamically generated data from the server), I need to use grid-template-columns: repeat(3, auto);
, and each cell must be a direct child of the wrapper div.
I've tried using nth-child to get the effect I want, but can't figure out what selector would let me target a dynamic range of elements.
Is there a better way to set up a grid layout using CSS Grid when the number of rows is unknown ahead of time?
Here's a fiddle with what I have so far, which includes all the code below. In my real project, the rows are dynamically generated and there are 5 columns right now- though that may change in the future.
.grid-table {
display: grid;
display: -ms-grid;
grid-template-columns: repeat(3, auto);
-ms-grid-template-columns: repeat(3, auto);
}
.grid-table :nth-child(-n+3) {
background-color: red;
}
<div class="grid-table">
<span>Room Name</span>
<span>Start Date</span>
<span>End Date</span>
<span>Room 100</span>
<span>Tuesday</span>
<span>Wednesday</span>
<span>Room 200</span>
<span>Tuesday</span>
<span>Friday</span>
<span>Room 200</span>
<span>Tuesday</span>
<span>Friday</span>
<span>Room 200</span>
<span>Tuesday</span>
<span>Friday</span>
</div>
These days, you could also use a an element with display:contents to contain cells (I've "grouped" them by rows or columns), and apply the style to cells that way.
.r_container {
display:grid;
grid-template-columns: repeat(3, 1fr);
width:200px;
justify-content:center;
align-items:center;
font-family:sans-serif;
}
.r_group {
display:contents;
}
.r_group:nth-of-type(2n) > .cell {
background-color:dimgray;
}
.cell {
justify-content:center;
align-items:center;
text-align:center;
padding-top:0.5rem;
padding-bottom:0.5rem;
}
.header > .cell {
background-color:black;
color:white;
font-weight:500;
}
<h1>Organized by Row</h1>
<div class="r_container">
<div class="r_group header">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
</div>
<div class="r_group">
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
</div>
<div class="r_group">
<div class="cell">7</div>
<div class="cell">8</div>
<div class="cell">9</div>
</div>
<div class="r_group">
<div class="cell">10</div>
<div class="cell">11</div>
<div class="cell">12</div>
</div>
</div>
Use a selector that targets every other row in one column.
For other columns, simply adjust and repeat.
.grid-table {
display: grid;
grid-template-columns: repeat(3, auto);
}
.grid-table > span:nth-child(-n+3) {
background-color: red;
}
.grid-table > span:nth-child(6n+4),
.grid-table > span:nth-child(6n+5),
.grid-table > span:nth-child(6n+6) {
background-color: lightgreen;
}
<div class="grid-table">
<span>Room Name</span>
<span>Start Date</span>
<span>End Date</span>
<span>Room 100</span>
<span>Tuesday</span>
<span>Wednesday</span>
<span>Room 200</span>
<span>Tuesday</span>
<span>Friday</span>
<span>Room 200</span>
<span>Tuesday</span>
<span>Friday</span>
<span>Room 200</span>
<span>Tuesday</span>
<span>Friday</span>
<span>Room Name</span>
<span>Start Date</span>
<span>End Date</span>
<span>Room 100</span>
<span>Tuesday</span>
<span>Wednesday</span>
<span>Room 200</span>
<span>Tuesday</span>
<span>Friday</span>
<span>Room 200</span>
<span>Tuesday</span>
<span>Friday</span>
<span>Room 200</span>
<span>Tuesday</span>
<span>Friday</span>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With