.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
}
.container-widest {
grid-template-columns: repeat(auto-fit, minmax(120px, 25%)); /* new */
width: 700px;
background-color: rgba(200, 0, 0, 0.3);
}
.container-wide {
grid-template-columns: repeat(auto-fit, minmax(120px, 200px)); /* new */
width: 600px;
background-color: rgba(200, 0, 0, 0.3);
}
.container-narrow {
width: 400px;
background-color: rgba(0, 200, 0, 0.4);
}
.container-narrower {
width: 300px;
background-color: rgba(0, 200, 0, 0.4);
}
/* style stuff */
html {
padding: 20px;
font-family: monospace;
}
.container {
margin: 0 auto;
margin-top: 20px;
border-radius: 3px;
}
.cell {
background: rgba(0, 0, 0, 0.1);
border: 1px solid rgba(0, 0, 0, 0.2);
border-bottom-width: 2px;
color: rgba(0, 0, 0, 0.4);
font-size: 26px;
padding: 40px;
text-align: center;
font-weight: 1;
border-radius: 3px;
}
p {
text-align: center;
padding-top: 50px;
}
<p>This ideally results in 4 cells per row to fill all blank spaces</p>
<div class="container container-widest">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
<div class="cell">7</div>
<div class="cell">8</div>
</div>
<p>For this configuration, aim for 3 cells per row</p>
<div class="container container-wide">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
</div>
<p>The layout functions smoothly here</p>
<div class="container container-narrow">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
</div>
<p>Another successful example!</p>
<div class="container container-narrower">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
</div>