What I am trying to accomplish:
There are 4 grid items. At the a widest screen size, I would like the items to be lined up in a row:
item_1 item_2 item_3 item_4
As the screen width shrinks, I would like the items to wrap to the next row like this:
item_1 item_2
item_3 item_4
Then finally at the narrowest, I would like the times to be a single columns
item_1
item_2
item_3
item_4
I found an example which does this but by only wrapping the next item when it can – https://labs.jensimmons.com/2017/03-009.html
Building off of that I tried using nested grid containers based on the example's model:
grid-template-columns: repeat(auto-fit, minmax(600px, 1fr));
HTML
.outer_grid {
display:grid;
grid-template-columns: repeat(auto-fit, minmax(600px, 1fr));
grid-template-row:1fr 1fr;
grid-gap:1em;
border:5px solid blue;
}
.grid {
display:grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
grid-template-row:1fr 1fr;
grid-gap:1em;
border:5px solid green;
height:200px;
}
.item {
border:2px solid red;
}
<div class="outer_grid">
<div class="grid">
<div class="item">item 1</div>
<div class="item">item 2</div>
</div>
<div class="grid">
<div class="item">item 3</div>
<div class="item">item 4</div>
</div>
</div>
It is nearly working (codepen below) but I'm not sure if this is the right approach or if there is a better way to accomplish this. I have tried using Flexbox too but chose CSS Grid because of the grid-gap feature. Also, I know it can be done but I am trying to do this without media queries.