Set your columns and rows, adjust the gaps, and watch the grid build itself. Track sizes cover fr units, fixed pixels, auto, min-content, max-content and minmax, or type your own. Identical tracks collapse into repeat() in the output, because repeat(12, 1fr) is what you would write and a twelve-item list is not something anyone wants to maintain. Switch on auto-fit for the most useful pattern in grid and the least well known: repeat(auto-fit, minmax(200px, 1fr)) fits as many columns as will hold 200px and stretches them to fill the row — a fully responsive grid with no media queries at all.
| Value | Meaning |
|---|---|
| 1fr | An equal share of the remaining space |
| 200px | Fixed, never changes |
| auto | As large as its content needs |
| min-content | As narrow as possible without overflowing |
| max-content | As wide as the content wants |
| minmax(200px, 1fr) | At least 200px, otherwise an equal share |
A fraction of the space left after fixed-size tracks are placed. 1fr 2fr gives the second track twice the leftover space. Unlike percentages, fr accounts for gaps automatically, which is why three 1fr columns with a gap fit exactly where three 33.33% columns overflow.
They behave identically until the grid is sparsely filled. auto-fill keeps the empty tracks, so three cards in a container with room for six sit in the left half with empty space beside them. auto-fit collapses the empty tracks, letting the three cards stretch across the full width. auto-fit is usually what people want.
Sets a floor and a ceiling for a track. minmax(200px, 1fr) means never narrower than 200px, otherwise take an equal share of the space. Combined with auto-fit it produces a responsive grid in a single declaration.
When you care about both dimensions. Grid aligns things into rows and columns simultaneously, so a card grid keeps its columns lined up regardless of content height. Flexbox handles one dimension at a time and is better for a row of buttons or a navigation bar.
Because grid creates implicit rows for any content beyond your explicit tracks, sized by their content. Defining rows explicitly only matters when you need specific heights or are placing items into named areas.
Because it is what you would write. repeat(3, 1fr) and 1fr 1fr 1fr are identical to the browser, but one is readable at twelve columns and the other is not.
Identical tracks are collapsed into repeat() rather than listed out, because repeat(12, 1fr) is what a person writes and a twelve-item list of 1fr is unreadable in a stylesheet someone has to maintain. Gaps collapse too: one value when the row and column gaps match, two when they differ, and nothing at all when both are zero. The auto-fit mode replaces the column list entirely rather than sitting alongside it, since repeat(auto-fit, minmax(…, 1fr)) is a complete specification of the columns and a fixed track list would contradict it. That pattern is the most useful thing in CSS grid and the least well known: it fits as many columns as will hold the minimum width and stretches them to fill the row, giving a fully responsive grid with no media queries. The distinction from auto-fill is worth stating, because they look identical until the grid is sparsely populated — auto-fill keeps the empty tracks, so three cards in a container with room for six sit in the left half with a gap beside them, while auto-fit collapses the empties and lets the cards stretch. An empty track size falls back to 1fr rather than emitting nothing, which would produce a grid-template-columns declaration with a missing value and invalidate the whole rule.