I'm trying to create a row of a non-fixed number of cells that presents the following properties:
- The cells should only be the size of their content, not more, not less;
- The row should only be the size of its cells (+ gap), not stretch or shrink under any circumstance);
- The row should break lines only when forced by the surrounding layout to prevent an overflow, much like
flex-wrap: row wrap
; - The row should "force" its size on the surrounding layout as long as nothing overflows (so if the row is brother to a
flex-grow: 1
div, the row shouldn't break its line); - If possible, all this without setting any style to the cells, and without media queries;
I can do that with flex
and row wrap
but I'm wondering if it's possible with display: grid
.
Here is what I tried :
.grid {
display: grid;
gap: 18px 18px;
grid-template-columns: repeat(auto-fit, minmax(0, auto));
border: 1px solid red;
}
.grid > * {
border: 1px solid blue;
}
<div class='grid'>
<div>aaa</div>
<div>aaaaa</div>
<div>aa</div>
<div>aaaa</div>
</div>
That's taking up too much space. Using auto-fill
makes it shrink too much. How could I do that?