You have the ability to create columns that repeat based on content size, but for a fixed number of columns. When using auto-fit
or auto-fill
, the grid will make all cells the same width.
In your scenario, it appears to be a 3x3 layout.
display: grid;
grid-template-columns: repeat(3, min-content);
*,
::after,
::before {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: grid;
place-items: center;
background-color: hsl(215, 100%, 98%);
position: relative;
overflow-x: hidden;
}
.container {
background-color: lightgray;
width: 760px;
display: grid;
grid-template-columns: repeat(3, min-content);
resize: horizontal;
overflow: hidden;
gap: 5px;
justify-items: center;
align-items: center;
}
.child {
background-color: gray;
display: flex;
flex-direction: column;
justify-content: center;
}
p {
text-align: center;
}
<div class="container">
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/80x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/120x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/160x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/80x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/120x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/160x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/80x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/120x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/160x75" alt="" />
</div>
</div>
A question was raised about the usage of repeat()
function with minmax()
. It was mentioned that while the first parameter specifies the minimal width, like 186px, the second determines how much it can expand.
display: grid;
grid-template-columns: repeat(auto-fit, minmax(186px, 1fr));
*,
::after,
::before {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: grid;
place-items: center;
background-color: hsl(215, 100%, 98%);
position: relative;
overflow-x: hidden;
}
.container {
background-color: lightgray;
width: 760px;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(186px, 1fr));
resize: horizontal;
overflow: hidden;
gap: 5px;
justify-items: center;
align-items: center;
}
.child {
background-color: gray;
display: flex;
flex-direction: column;
justify-content: center;
}
p {
text-align: center;
}
<div class="container">
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/80x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/120x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/160x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/80x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/120x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/160x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/80x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/120x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/160x75" alt="" />
</div>
</div>