Basically, I do not understand why this works:
.grid {
display: grid;
grid-template-columns: repeat(4, min-content);
}
But this doesn't work:
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, min-content);
}
I really wish to make the latter functionality possible. Are there other ways to make it work?
The browser will allow empty columns to occupy space in a row. The content will stretch to fill the entire row width. Grid layout remains fixed with or without items. Grid layout is not fixed ,the content stretch to fit entire width.
auto-fit behavior: “make whatever columns you have fit into the available space. Expand them as much as you need to fit the row size. Empty columns must not occupy any space. Put that space to better use by expanding the filled (as in: filled with content/grid items) columns to fit the available row space.”
Just use width: 100% and height: 100% in the CSS class of the item you want to fill the grid. Join a max-width property and a max-height property if you don't want a grid item inside a grid container to grow more than some size.
With auto-fill , everything is the same as auto-fit , except empty tracks are not collapsed. They are preserved. Basically, the grid layout remains fixed, with or without items. That's the only difference between auto-fill and auto-fit .
The second rule doesn't work because min-content
is an intrinsic sizing function.
- Automatic repetitions (
auto-fill
orauto-fit
) cannot be combined with intrinsic or flexible sizes.
An intrinsic sizing function (
min-content
,max-content
,auto
,fit-content()
).A flexible sizing function [is a dimension with the
fr
unit].
I've worked around this by doing
grid-template-columns: repeat(auto-fill, minmax(0, max-content));
This ensures that the grid tracks are created with a minimum size that is not "intrinsic" whilst allowing the tracks to expand to fit based on the max-content
. I use auto-fit
instead of auto-fill
sometimes depending on use case, but both should work.
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