Grid & sizing

The functions that size grid tracks in grid-template-columns, grid-template-rows and grid-auto-columns/grid-auto-rows. With them a grid can set how many tracks it has, how far each may grow and shrink, and how closely it follows its content. The grid properties themselves are under Grid; for arithmetic on sizes see calc-size() and clamp() under Math.

Track sizing

repeat()

Repeats a pattern of track sizes, and optionally line names, a set number of times, or as many times as fit with auto-fill or auto-fit. Allowed only in grid-template-columns and grid-template-rows.

Syntaxrepeat( [ <integer [1,∞]> ] , [ <line-names>? <track-size> ]+ <line-names>? )
repeat( [ auto-fill | auto-fit ] , [ <line-names>? <fixed-size> ]+ <line-names>? )
ReturnsA run of track sizes and line names within a track list
SupportChrome 57 · Firefox 76 · Safari 10.1
Speccss-grid-2
CountEffect
An integerRepeats the pattern exactly that many times.
auto-fillRepeats as many times as fit in the container without overflowing. Tracks with no items stay, holding their space.
auto-fitThe same, but empty repeated tracks collapse to zero, so the filled tracks can stretch across the whole container.

With auto-fill or auto-fit, every size in the pattern must be definite (a <fixed-size>), at least as its minimum, so the browser can work out how many fit. Only one automatic repeat is allowed per track list.

grid-template-columns: repeat(12, 1fr);
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
A twelve-column grid, and a responsive card grid that fits as many 16rem columns as it can.

minmax()

A track size range: the track is never smaller than the first value and never larger than the second. If the maximum is smaller than the minimum, the maximum is ignored.

Syntaxminmax( <inflexible-breadth> , <track-breadth> )
Returns<track-size>
SupportChrome 57 · Firefox 52 · Safari 10.1
Speccss-grid-2

The minimum (<inflexible-breadth>) is a length, percentage, min-content, max-content or auto; it cannot be an fr value. The maximum (<track-breadth>) can be any of those or an fr value.

grid-template-columns: minmax(12rem, 20rem) minmax(0, 1fr);
A sidebar between 12rem and 20rem, and a main column that takes the rest and can shrink below its content's width.

A plain 1fr track has an automatic minimum, so long content can force it wider than its share. minmax(0, 1fr) removes that minimum.

fit-content()

A track that grows to fit its content, up to the given limit. It behaves like minmax(auto, max-content), except that the track never grows past the argument even if its content is wider.

Syntaxfit-content( <length-percentage> )
Returns<track-size>
SupportChrome 57 · Firefox 52 · Safari 10.1
Speccss-grid-2
grid-template-columns: fit-content(15rem) 1fr;
A first column as wide as its content, but no wider than 15rem.