CSS Grid

Quick reference for CSS Grid: track sizing, why 1fr is minmax(auto, 1fr), minmax(0, 1fr), and auto-fit versus auto-fill.

Grid is two-dimensional: you define rows and columns, then place items on those tracks. The CSS tutorial already uses .cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; } and a responsive 1fr / minmax(0, 1fr) switch. This page explains those track functions and does not replace the #layout or #responsive lessons.

A flex-like 1fr is not “just a fraction of leftover space.” In CSS Grid, a standalone <flex> value is treated as minmax(auto, 1fr), so the track’s minimum is the automatic (often content-based) minimum. That is why long unbreakable content can keep a 1fr column from shrinking, and why the tutorial writes minmax(0, 1fr).

Examples were parsed for syntax and structure. Track sizes were not measured in a browser.

Official sources: MDN and the CSS specification. Fragments were checked with PostCSS 8 for syntax and structure. That is not a CSS conformance suite, and this is not a browser render or visual test.

CSS reference catalog → · CSS tutorial overview →

Track sizing and minmax(0, 1fr)

fr units share leftover space after definite tracks and gaps are accounted for. minmax(min, max) clamps a track between two sizes. minmax(0, 1fr) lets the track shrink to zero and grow as a fraction, which is usually what you want for equal, shrinkable columns in a card grid.

The tutorial also sets min-width: 0 and overflow-wrap on the cards themselves. Track minimums and item minimums are related: a 1fr track with an auto minimum, or a grid item with min-width: auto, can both overflow the grid container.

Syntax or propertyInitial / defaultNotes
display—grid or inline-grid (or the two-value forms block grid and inline grid).
grid-template-columnsnoneColumn track list. Lengths, fr, minmax(), repeat(), and keywords such as auto and max-content are common.
grid-template-rowsnoneRow track list, with the same functions as columns.
gap0Shorthand for row-gap and column-gap between tracks. The tutorial uses gap instead of item margins.
minmax()—minmax(min, max) sets a track’s lower and upper bounds. minmax(0, 1fr) is a shrinkable fractional track. A bare 1fr is minmax(auto, 1fr).
repeat()—Repeats a track list. The first argument may be a count, auto-fill, or auto-fit.
justify-items / align-itemsnormal / normalDefault alignment of items in their cells. stretch is the usual used value for normal in a grid cell, unless the item has an intrinsic size that prevents it.

Equal columns with minmax(0, 1fr)

grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 16px;

Two shrinkable fractional columns, the same pattern as the tutorial card grid.

  • A declaration of repeat(2, 1fr) would be two minmax(auto, 1fr) tracks, whose auto minimum can resist shrinking.
html
<div class="cards">
  <article class="card">Short text</article>
  <article class="card">docs/garden/week-01/planting-notes</article>
</div>
<style>
.cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.card { min-width: 0; overflow-wrap: anywhere; }
</style>

What this markup means

Each column can shrink to 0 and then share leftover space as 1fr. The card also sets min-width: 0 so the grid item itself can shrink. The long path is documentation text, not a file read. This matches the tutorial’s minmax(0, 1fr) choice rather than a bare 1fr.

Related: CSS tutorial: layout, CSS tutorial: responsive, Flexbox

MDN documentation → · CSS specification →

auto-fit versus auto-fill

repeat(auto-fill, minmax(12rem, 1fr)) packs as many tracks of at least 12rem as will fit in the container, including empty tracks if there are not enough items to fill them. Empty auto-fill tracks stay in the grid, so the occupied tracks do not necessarily grow to fill the entire row.

repeat(auto-fit, minmax(12rem, 1fr)) uses the same packing, then collapses empty tracks to 0. The remaining tracks can therefore grow with their 1fr maximum and fill the row. With a completely full row, auto-fit and auto-fill look the same. The difference appears when there are leftover empty tracks.

Neither keyword is a media query. The number of tracks still depends on the container size and the minmax() minimum. This page does not count those tracks in a browser.

Syntax or propertyInitial / defaultNotes
auto-fill—Repeat as many tracks as fit, and keep empty tracks. Items do not always expand to consume leftover space left in those empty tracks.
auto-fit—The same repetition, but empty tracks are collapsed. Remaining 1fr tracks can grow to fill the row.

The same minmax list with auto-fill and auto-fit

repeat(auto-fill, minmax(12rem, 1fr))
repeat(auto-fit, minmax(12rem, 1fr))

auto-fill keeps empty tracks. auto-fit collapses them so remaining tracks can grow.

  • Both lists use minmax(12rem, 1fr), not a bare 1fr, so each track still has a definite minimum of 12rem.
html
<div class="fill">
  <article>One</article>
  <article>Two</article>
</div>
<div class="fit">
  <article>One</article>
  <article>Two</article>
</div>
<style>
.fill, .fit { display: grid; gap: 16px; }
.fill { grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr)); }
.fit { grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)); }
</style>

What this markup means

Each grid has only two items. In a container wide enough for three or more 12rem tracks, the auto-fill grid keeps the unused tracks, while the auto-fit grid collapses them and lets the two items grow. In a container that only fits two tracks, both rules can look the same.

Related: CSS tutorial: responsive

MDN documentation → · CSS specification →