I have something like this :
.row {
width: 300px;
border: 1px solid black;
display: flex;
}
.otherstuff {
flex-grow: 1;
}
.grid {
display: grid;
grid-auto-flow: column;
gap: 10px 10px;
}
.cell {
border: 1px solid blue;
}
<div class='row'>
<div class='stuff'>Stuff</div>
<div class='otherstuff'>
<div class='grid'>
<div class='cell'>Cell 1</div>
<div class='cell'>Cell 2</div>
</div>
</div>
</div>
I would like the grid to take up only the minimal amount of space required, and not to stretch out. I've read this thread and saw that it was possible with grid-template-columns
and auto
values, but is there a way to do it without knowing the number of children in advance ?
Thank you.
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.
auto-fit FITS the CURRENTLY AVAILABLE columns into the space by expanding them so that they take up any available space. The browser does that after FILLING that extra space with extra columns (as with auto-fill ) and then collapsing the empty ones.
Building a Responsive Grid-View First ensure that all HTML elements have the box-sizing property set to border-box . This makes sure that the padding and border are included in the total width and height of the elements. Read more about the box-sizing property in our CSS Box Sizing chapter.
As per the CSS spec, the definition of minmax(min, max) is as the following: Defines a size range greater than or equal to min and less than or equal to max. Let's analyze the above: We have three grid columns.
You can use grid-auto-columns: min-content
or grid-auto-columns: max-content
, this property will affect only columns with the size not set.
The
grid-auto-columns
CSS property specifies the size of an implicitly-created grid column track.
.row {
width: 300px;
border: 1px solid black;
display: flex;
}
.otherstuff {
flex-grow: 1;
}
.grid {
display: grid;
grid-auto-flow: column;
gap: 10px 10px;
grid-auto-columns: max-content;
}
.cell {
border: 1px solid blue;
}
<div class='row'>
<div class='stuff'>Stuff</div>
<div class='otherstuff'>
<div class='grid'>
<div class='cell'>Cell 1</div>
<div class='cell'>Cell 2</div>
</div>
</div>
</div>
You can use inline-grid
- see demo below:
.row {
width: 300px;
border: 1px solid black;
display: flex;
}
.otherstuff {
flex-grow: 1;
}
.grid {
display: inline-grid; /* changed */
grid-auto-flow: column;
gap: 10px 10px;
}
.cell {
border: 1px solid blue;
}
<div class='row'>
<div class='stuff'>Stuff</div>
<div class='otherstuff'>
<div class='grid'>
<div class='cell'>Cell 1</div>
<div class='cell'>Cell 2</div>
</div>
</div>
</div>
Do the flex-grow: 0
in otherstuff
class. See below:
.row {
width: 300px;
border: 1px solid red;
display: flex;
}
.otherstuff {
flex-grow: 0;
}
.grid {
display: grid;
grid-auto-flow: column;
gap: 10px 10px;
}
.cell {
border: 1px solid blue;
}
<div class='row'>
<div class='stuff'>Stuff</div>
<div class='otherstuff'>
<div class='grid'>
<div class='cell'>Cell 1</div>
<div class='cell'>Cell 2</div>
</div>
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With