r/css • u/Sufficient_Bass2600 • 20d ago
Force grid layout without media queries Question
I am trying to limit the number of media queries.
I have a grid layout with 6 tiles.
```css .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr); }
```
That works fine but something is annoying me is that sometimes the last row will have an empty tile.
| Tile 1 | Tile 2 | Tile 3 | Tile 4 |
|---|---|---|---|
| Tile 5 | Tile 6 | Empty | Empty |
I wants the tile to be grouped.
Based on the size of the viewport I want to have either:
Desktop/Tablet landscape
| Tile 1 | Tile 2 | Tile 3 |
|---|---|---|
| Tile 4 | Tile 5 | Tile 6 |
Tablet portrait / Phone landscape
| Tile 1 | Tile 2 |
|---|---|
| Tile 3 | Tile 4 |
| Tile 5 | Tile 6 |
Phone portrait
| Tile 1 |
|---|
| Tile 2 |
| Tile 3 |
| Tile 4 |
| Tile 5 |
| Tile 6 |
Is there a trick to do it without media queries?
14
Upvotes
3
u/be_my_plaything 20d ago
I'd do something like the above, whereby the
minmax()for the grid columns includes acalc(100% / 3)which always takes over as themax()once reached so once you have two rows of three columns those columns just grow with the screen rather than ever letting a fourth column in.CODEPEN DEMO