Why Doesn't Min-Content Work with Auto-Fill or Auto-Fit

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

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

css grid-column span doesn't work well with auto-fit or auto-fill

You need to add a media query to remove the span 2 which is forcing your grid to always have 2 columns with the second one empty

.grid {
gap: 20px;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
}

.box,
.special-box {
height: 200px;
}

.box:nth-child(even) {
background-color: pink;

}

.box:nth-child(odd) {
background-color: orange;

}

.special-box {
background-color: purple;
}

@media (min-width:650px) {
.special-box {
grid-column: span 2;
}
}
<div class="grid">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="special-box"></div>
</div>

CSS Grid auto fit with max-content

I had a similar question when playing around with grid:

grid-template-columns: repeat(auto-fit, minmax(max-content, 1fr))

If we take a look at the documentation we can see that minmax command is valid:
https://developer.mozilla.org/en-US/docs/Web/CSS/minmax

But in a repeat documentation on csswg, it states one simple rule that disallows all of this from happening;
https://drafts.csswg.org/css-grid/#funcdef-repeat

The generic form of the repeat() syntax is, approximately,

repeat( [ <positive-integer> | auto-fill | auto-fit ] , <track-list> )

The first argument specifies the number of repetitions. The second
argument is a track list, which is repeated that number of times.

However, there are some restrictions:

  • The repeat() notation can’t be nested.

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

Whats an intrinsic or flexible sizes ?

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

So the command wont work in grid because each column/row will be different sizes and wrapping cannot take place. See bellow picture as example.

minmax max-content auto-fit

This behavior should be executed using flex-box instead.

CSS - grid auto-fit not working when using minmax with max being specific length

Is auto-fill or just auto be what you would expect ?

.working {
width: 100%;
height: 5rem;
margin-bottom: 3rem;
justify-content: start;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(5rem, auto));
gap: 1rem;
}

.item {
background-color: red;
min-width:min-content;
}
.not.working {

grid-template-columns: repeat(auto-fit, minmax(5rem, auto));
}
<div class='working'>
<div class='item'>plap plap plap plap</div>
<div class='item'>plip plip</div>
<div class='item'>plop plop plop plop plop plop plop plop plop plop</div>
<div class='item'>.</div>
</div>

<div class='not working'>
<div class='item'>plap plap plap plap</div>
<div class='item'>plip plip</div>
<div class='item'>plop plop plop plop plop plop plop plop plop plop</div>
<div class='item'>.</div>
</div>

Grid child elements won't wrap despite using auto-fit/fill and explicit values

You will need to reset your column template from 2 (or more with autofill) to a real single one via a mediaquerie.

repeat(auto-fill, minmax(24rem, 1fr) 19rem) sets a minimum of 2 columns , one of them has a fixed size of 19rem. If more contents it would turn into 4 columns , and so on :)

possible example :

.main {
max-width:1000px;
background-color:skyblue;
margin-left:auto;
margin-right:auto;
}

.parent {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(24rem, 1fr) 19rem);
padding: 20px;
}

.parent .child:first-child {
background-color: #eeeeee;
height: 300px;
}
.parent .child:last-child {
background-color: #cccccc;
height: 300px;
}
@media (max-width : 600px) {
.parent {
grid-template-columns: 1fr;
}
}
<div class="main">
<div class="parent">
<div class="child">first</div>
<div class="child">second</div>
</div>
</div>

Why is auto-fill property of CSS Grid not working in column direction

To wrap grid in vertical direction you have to do a bit more:

  • specify a height for the grid container so that the grid items know when to wrap,

  • also specify grid-auto-flow: column (overriding default grid-auto-flow: row)

See demo below (have set height: 100% for illustration):

body,html {  height: 100%;  margin: 0;}
.wrapper { display: grid; grid-template-rows: repeat(auto-fill, minmax(140px, 200px)); grid-auto-flow: column; /* added */ height: 100%; /* adjust this*/}
.wrapper>div:nth-child(odd) { background-color: red;}
<div class="wrapper">  <div class="one"> 1 </div>  <div class="one"> 2 </div>  <div class="one"> 3 </div>  <div class="one"> 4 </div>  <div class="one"> 5 </div></div>

auto-fit and minmax() don't work inside nested grid

The problem is that auto-fit and minmax functions don't work in the nested grid.

I think they do work. The problem appears to be something else.

Your nested grid exists in a column with a fixed width (700px). The primary container sees no reason to shrink that column, which would trigger the auto-fit function in the nested grid.

Here's something you may want to consider:

revised codepen