WebCraft

minmax(min())

A responsive CSS Grid that fits narrow containers

A grid that gives up its minimum first

Live demo

Resize the box from its bottom corner.

  • 01Research
  • 02Prototype
  • 03Test
  • 04Ship

Caveat and fallback

CaveatA fixed minimum in minmax(9rem, 1fr) is still nine rem when its parent is only eight rem wide. Wrap the minimum in min(100%, 9rem) and the track can shrink to the container instead of forcing horizontal scrolling. auto-fit adds columns when they fit and collapses empty tracks. Resize this demo on desktop or narrow the browser on a phone; no viewport breakpoint decides the column count. An unbroken URL can still overflow the item, so the children also get min-width: 0 and a wrapping rule.

FallbackA plain list is the baseline if Grid is unavailable. The resize handle is only a desktop demonstration aid, not a required control: at any viewport width the list lays itself out. Percentage track sizing assumes the wrapper has a definite available width; intrinsic shrink-to-fit contexts need separate testing. For the empty-track difference, compare auto-fit and auto-fill. Reference: MDN documentation.

Source code

HTML + CSS
<div class="d-fluidgrid">
  <p>Resize the box from its bottom corner.</p>
  <ul>
    <li><b>01</b>Research</li>
    <li><b>02</b>Prototype</li>
    <li><b>03</b>Test</li>
    <li><b>04</b>Ship</li>
  </ul>
</div>

.d-fluidgrid {
  width: 100%; max-width: 30rem;
  min-width: min(10rem, 100%);
  resize: horizontal; overflow: auto;
  color: var(--paper, #e6eaf1);
  border: 1px solid #64748b;
  padding: .75rem;
}
.d-fluidgrid p {
  margin: 0 0 .75rem; font-size: .8rem;
}
.d-fluidgrid ul {
  display: grid; gap: .5rem;
  grid-template-columns:
    repeat(auto-fit, minmax(min(100%, 9rem), 1fr));
  list-style: none; margin: 0; padding: 0;
}
.d-fluidgrid li {
  min-width: 0; overflow-wrap: anywhere;
  border: 1px solid #64748b;
  padding: .8rem;
  background: rgb(111 168 220 / .12);
}
.d-fluidgrid b {
  display: block; color: #6fa8dc;
  font-size: 1.5rem; margin-bottom: .5rem;
}

Published