When you are not using minmax
, css grid can use content-sensitive sizing, but evidently, as soon as you add minmax
, the grid ignores content and divides space equally among different cells. minmax
also prefers to waste space if possible; it will only use less than the maximum if the container is constrained (e.g. by a fixed height
or width
.)
Therefore, avoid minmax
if you want content-sensitive sizing. In fact, content-sensitive sizing seems to be supported only for auto
(and no, you cannot write something like calc(auto+2fr)
to get more intelligent distribution of free space.)
The desired effect of having a minimum and maximum row height can be accomplished by using auto
as the row height and setting the minimum and maximum as properties of the items instead:
.testgrid {
display: grid;
grid-template-columns: 20% auto 1fr;
grid-auto-rows: auto;
max-width: 600px;
}
.testgrid .item {
background-color: #AF6;
margin: 1px;
overflow: hidden;
min-height: 1.5em;
max-height: 4.5em;
}
<div class="testgrid">
<div class="item">Cell 1</div>
<div class="item">Cell 2</div>
<div></div>
<div class="item">Cell 3 (uses too much space, so really it ought to be clipped)</div>
<div class="item">Cell 4</div>
<div></div>
<div class="item">Cell 5</div>
<div class="item">Cell 6 (more text here, could push the column wider)</div>
<div></div>
</div>
I couldn't find an easy way to use less than the maximum column width (this is strange because HTML tables behave that way by default, so it is surprising if the css grid system, which would have ideally made table layouts obsolete, doesn't offer a feature for this purpose.) justify-items: start
(or justify-self: start
in .item
) causes individual cells to shrink independently of each other, but that's undesirable if you want all cells in the column to have the same width.
As a workaround I added a dummy third column to occupy the unused space. Unfortunately this is not CSS-only; the HTML must be aware of the dummy column. In order for this to work properly, the dummy column needs to use the fr
unit while all other columns must be fixed-width or auto
.