.small-container { width: 200px; }
.small-container > div { margin: 16px; }
.border { box-shadow: 0 0 2px 2px black; }
.break-long-text {
width: 100%;
word-wrap: break-word;
overflow-wrap: break-word;
}
.grid-fr {
display: grid;
grid-template-columns: 1fr;
}
.grid-minmax {
display: grid;
grid-template-columns: minmax(120px, 1fr);
}
<div class="small-container">
<div class="grid-fr">
<div class="break-long-text border">
LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG
</div>
</div>
<div class="grid-minmax">
<div class="break-long-text border">
LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG
</div>
</div>
</div>
The two grids are completely the same, the only difference is the grid-template-columns
value. However, one grid respects the width of its parent (200px
) and the other ignores the width.
grid:
grid-template-columns: 1fr;
=> ignores parent-width and continues until the content is filledgrid:
grid-template-columns: minmax(120px, 1fr);
=> Only goes up to parent-width (200px
) and then becomes as high as necessary. However, it is still larger than the defined min-width (120px
), which is of course correct since1fr
is larger than120px
in this example.
Since in the end both should have the same width of 1fr
, I wonder about different results. Is this a bug or expected behavior? And why does the second grid work the way I expect it to and not the first?
So should I always use minmax
if I don't want one cell to be larger than its parent?