Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't min-content work with auto-fill or auto-fit?

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?

like image 710
timetowonder Avatar asked Sep 25 '18 17:09

timetowonder


People also ask

What is auto fill in grid?

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.

How do you use Auto-fit grid?

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.”

How do you fill a grid with 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.

What is the difference between auto-fit and auto fill?

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 .


2 Answers

The second rule doesn't work because min-content is an intrinsic sizing function.

§ 7.2.2.1. Syntax of repeat()

  • Automatic repetitions (auto-fill or auto-fit) cannot be combined with intrinsic or flexible sizes.

§ 11. Grid Sizing

  • An intrinsic sizing function (min-content, max-content, auto, fit-content()).

  • A flexible sizing function [is a dimension with the fr unit].

like image 99
Michael Benjamin Avatar answered Sep 17 '22 15:09

Michael Benjamin


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.

like image 35
eriklharper Avatar answered Sep 20 '22 15:09

eriklharper