Skip to content
IndexRequest a UIAboutContact

Auto-fit Grid with minmax

A grid that picks its own column count from a minimum card width, with auto-fit versus auto-fill on a half-empty row.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI IMAGE OR VIDEO TOOL
Diagram of a responsive CSS grid shown at two container widths one above the other, four rounded pastel cards in an even grid, in the wide container the cards stretch to fill the row with auto-fit and in an alternative panel they stay the same size with an empty dashed column on the right for auto-fill, thin dashed outlines for the container, small monospace labels showing repeat(auto-fit, minmax(120px, 1fr)), clean flat vector style. Negative: gradients, shadows, text errors, clutter. Reduced motion: static diagram.

About this pattern

Definition

grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)) builds as many columns as fit with a minimum size, and shares leftover space between them. It makes a responsive card grid with no media queries. The difference between auto-fit and auto-fill only appears when there are fewer items than columns.

When to use it

  • minmax(min, 1fr): each column is at least min wide and shares leftover space equally.
  • auto-fit collapses empty tracks, so the few items stretch to fill the row.
  • auto-fill keeps empty tracks, so items stay at their natural size and leave gaps.
  • Pick a minimum that is the smallest comfortable card width.

What the controls do

  • Container width and Minimum card width sliders, auto-fit and auto-fill buttons.

Motion & timing

  • Columns change instantly.
  • Reduced motion: nothing animates.

Tips & variations

  • Use min(100%, 120px) inside minmax so a card never overflows a very narrow container.
  • Pair with gap for spacing.

Code of this live preview

HTML
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Mode"><button data-p="0" aria-pressed="true">auto-fit</button><button data-p="1" aria-pressed="false">auto-fill</button></div><div class="hd" id="hd"><div class="gd" id="gd"><i>1</i><i>2</i><i>3</i><i>4</i></div></div><div class="two"><label class="rw">Container <b id="cv">330px</b><input id="cw" type="range" min="150" max="330" value="330"></label><label class="rw">Min card <b id="mv">70px</b><input id="mn" type="range" min="50" max="140" value="70"></label></div><pre class="cs" id="cs"></pre></div>

Related patterns

Bento Grid

↗
Bento GridLayout Structures

Masonry Columns Layout

↗
Masonry Columns LayoutLayout & Scrolling

Container Query Cards

↗
Container Query CardsLayout & Scrolling