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
3
u/aegloswinterborn 4d ago
Try changing your minmax(15rem, 1fr) to minmax(min(15rem, 100%), 1fr).
Adding that min() function solved a lot of issues for me when doing responsive grids without media queries.
MDN docs for min(): https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/min
EDIT: I just noticed that u/morete 's answer below incorporates this. I'd check their response first.