<div class="wrapper">
    <article class="flow">
      <h1>Grid dense and sparse packing</h1>
      <figure class="callout">
        <p>Change the packing mode to see how this changes the layout of items.</p>
      <div class="controls">
        <label class="toggle" for="gridPacking">
          <span class="toggle__label">Turn on dense packing</span>
          <input type="checkbox" role="switch" class="toggle__element" id="gridPacking">
          <div class="toggle__decor" aria-hidden="true">
            <div class="toggle__thumb"></div>
      <div class="container" id="container">
        <div class="box span-row">Item one</div>
        <div class="box">Item two</div>
        <div class="box span-col">Item three has more text </div>
        <div class="box">Item four</div>
        <div class="box">Item five</div>
:root {
  --var-flow: row;
  --var-mode: horizontal-tb;

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-auto-rows: minmax(100px, auto);
  grid-auto-flow: var(--var-packing);
  gap: 10px;
  background-color: var(--color-stroke);
  padding: 1em;
  max-width: 30rem;

.span-row {
  grid-row: auto / span 2;

.span-col {
  grid-column: auto / span 2;
let root = document.documentElement;

let packing = document.getElementById("gridPacking");
packing.addEventListener("click", function (evt) {
  if(packing.checked) {
    root.style.setProperty('--var-packing', 'row dense');
  } else {
    root.style.setProperty('--var-packing', 'row');

External CSS

  1. https://codepen.io/web-dot-dev/pen/abpoXGZ.css

External JavaScript

This Pen doesn't use any external JavaScript resources.