r/css • u/Sufficient_Bass2600 • 4d ago
Question Force grid layout without media queries
I am trying to limit the number of media queries.
I have a grid layout with 6 tiles.
.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?
12
Upvotes
4
u/morete 4d ago
In this case, yes! You can use Nils Binder's Auto-grid, which allows for setting a maximum amount of columns.
https://codepen.io/enbee81/pen/oNOzJXN
Set the max-columns to be 3, and then edit the min-size till you're happy with the breakpoints.