<p>By default, <code>grid-auto-flow</code> uses the sparse algorithm to place grid items. Check the checkbox to change it to dense and see how the grid items become tightly packed.</p>
<input type="checkbox" class="flow-switcher" id="dense">
<label for="dense">grid-auto-flow: dense</label>
<div class="container">
  <div class="grid__item a">A</div>
  <div class="grid__item b">B</div>
  <div class="grid__item c">C</div>
  <div class="grid__item d">D</div>
  <div class="grid__item e">E</div>
  <div class="grid__item f">F</div>
  <div class="grid__item g">G</div>
  <div class="grid__item h">H</div>
  <div class="grid__item i">I</div>
  <div class="grid__item j">J</div>
  <div class="grid__item k">K</div>
  <div class="grid__item l">L</div>
  <div class="grid__item m">M</div>
  <div class="grid__item n">N</div>
</div>
.container {
  margin: 40px auto;
  max-width: 700px;
  
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  grid-gap: 1em;
}

.flow-switcher:checked ~ .container {
  grid-auto-flow: dense;
}

.grid__item {
  background-color: lightgreen;
  padding: 1em;
}

.c {
  grid-row: span 2;
}

.j {
  grid-column: span 3;
}

.g {
  grid-column: span 3;
  grid-row: span 2;
}

.n {
  grid-column: span 2;
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.