Define rows and columns in an explicit grid.
The rows and columns you define with template properties make up the explicit grid. Each track can have a fixed size, fit its content, or share available space.
.page {
display: grid;
grid-template-columns: 12rem 1fr;
grid-template-rows: 4rem auto 3rem;
gap: 1rem;
}Each space-separated size defines one track:
12rem column followed by a flexible 1fr column.4rem row, an automatic row, and a 3rem row.This defines two columns and three rows. It does not automatically make an item span both columns; placement is a separate decision.
With an 800-pixel content width and a 16-pixel root font size, the first column uses 192 pixels and the gap uses 16. The flexible column receives the remaining 592 pixels when content minimums do not force it wider.
A grid needs a 10rem column followed by two equal flexible columns. Finish the list after the first size:
.layout {
display: grid;
grid-template-columns: 10rem /* add two tracks */;
}Use 10rem 1fr 1fr. Each value defines one track.
The repeat() function takes a count and a track pattern. How would you repeat 1fr four times, and how many columns would eight short items share?
repeat(4, 1fr) defines four columns. Eight items can fill two rows; the repeat count describes tracks, not items.
| Track value | Behavior |
|---|---|
12rem | A fixed length relative to the root font size. |
auto | Uses automatic sizing that considers the items' content and available space. |
1fr | Receives a share of leftover space, subject to its minimum. |
Fixed row heights can be useful for a deliberate size, but text can outgrow them. The auto middle row can respond to its content. If a container has extra height, automatic rows can also stretch under the default content alignment.
fr rows share space in the grid, not the viewport. Give the container a set height when you want rows to divide a known amount of vertical space.
.three-columns {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}repeat(3, 1fr) writes 1fr 1fr 1fr compactly. The count repeats the supplied track pattern; it does not limit the total number of items to three.
If six short articles use this grid, automatic placement fills two rows while preserving the same three column boundaries. Rows not explicitly listed are created as needed; the Implicit Grids lesson covers their sizing.
Two 50% columns already consume the container's full width. Adding a nonzero gap can make the tracks overflow because the percentages do not automatically subtract the gap.
Use fr to divide the space left after gaps. A plain 1fr track still has a content-based minimum, so long text can make it wider. Flexible Grids shows how minmax() changes the minimum.
repeat() abbreviates a track pattern, not an item limit.Try it yourself
HTML + CSS