<h2>Auto-fit</h2>
<div class="auto-fit">
<div class="child">A</div>
<div class="child">B</div>
<div class="child">C</div>
<div class="child">D</div>
</div>
<h2>Auto-fill</h2>
<div class="auto-fill">
<div class="child">A</div>
<div class="child">B</div>
<div class="child">C</div>
<div class="child">D</div>
</div>
.auto-fit {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
grid-gap: 1em;
}
.auto-fill {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
grid-gap: 1em;
}
/* for demo purpos */
.child {
background: pink;
min-height: 100px;
display: flex;
justify-content: center;
align-items: center;
font-size: 3rem;
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.