Grid
The properties that define a grid container's rows and columns, place items on its lines and areas, and align items in their cells. An element becomes a grid container with display: grid or display: inline-grid (see Box model). Track sizing functions such as repeat() and minmax() are under Grid sizing functions; the spacing between tracks is under Gaps & gap decorations; justify-content, align-content, align-items, align-self and order are under Flexbox.
Templates
grid
Shorthand that sets the whole grid in one declaration: either a grid-template value, or an explicit template in one axis with auto-flow tracks in the other. Any longhand it does not set is reset to its initial value.
| Syntax | <'grid-template'> | <'grid-template-rows'> / [ auto-flow && dense? ] <'grid-auto-columns'>? | [ auto-flow && dense? ] <'grid-auto-rows'>? / <'grid-template-columns'> |
|---|---|
| Initial | none |
| Applies to | Grid containers |
| Inherited | See individual properties |
| Percentages | See individual properties |
| Animation | See individual properties |
| Support | Chrome 57 · Firefox 52 · Safari 10.1 |
| Spec | css-grid-2 |
grid: auto-flow / repeat(3, 1fr);
grid-template
Shorthand for grid-template-rows, grid-template-columns and grid-template-areas. Give rows and columns separated by a slash, or write the area strings with each row's size after its string and the column sizes after the slash.
| Syntax | none | [ <'grid-template-rows'> / <'grid-template-columns'> ] | [ <line-names>? <string> <track-size>? <line-names>? ]+ [ / <explicit-track-list> ]? |
|---|---|
| Initial | none |
| Applies to | Grid containers |
| Inherited | See individual properties |
| Percentages | See individual properties |
| Animation | See individual properties |
| Support | Chrome 57 · Firefox 52 · Safari 10.1 |
| Spec | css-grid-2 |
grid-template:
"head head" auto
"side main" 1fr
/ 12rem 1fr;
grid-template-columns, grid-template-rows
The explicit grid's track sizes in one axis, as a space-separated list, optionally with line names in square brackets between them ([full-start] 1fr [content-start]). Tracks can be lengths, percentages, fr units of free space, auto, min-content, max-content, minmax() or fit-content(), and repeat() repeats a run of them.
| Syntax | none | <track-list> | <auto-track-list> | subgrid <line-name-list>? |
|---|---|
| Initial | none |
| Applies to | Grid containers |
| Inherited | No |
| Percentages | Refer to the corresponding dimension of the content area |
| Animation | If the list lengths match, by computed value type per item in the computed track list; discrete otherwise |
| Support | Chrome 57 · Firefox 52 · Safari 10.1subgrid: Chrome 117 · Firefox 71 · Safari 16 |
| Spec | css-grid-2 |
An <auto-track-list> contains one repeat(auto-fill, …) or repeat(auto-fit, …), which repeats as many times as fit; auto-fit then collapses the empty repeated tracks. subgrid makes a nested grid use its parent's tracks in that axis instead of defining its own.
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
grid-template-areas
Names areas of the grid with strings, one string per row and one name per cell. Repeating a name across adjacent cells makes a larger area, which must be a rectangle; a . marks an unnamed cell. Items are then placed by name with grid-area.
| Syntax | none | <string>+ |
|---|---|
| Initial | none |
| Applies to | Grid containers |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 57 · Firefox 52 · Safari 10.1 |
| Spec | css-grid-2 |
Each named area also creates line names: an area called main gives lines main-start and main-end in both axes.
Implicit grid
grid-auto-columns, grid-auto-rows
The size of tracks the grid creates by itself, when items are placed beyond the explicit template or more items arrive than it has cells. A list of sizes is used in turn and repeats.
| Syntax | <track-size>+ |
|---|---|
| Initial | auto |
| Applies to | Grid containers |
| Inherited | No |
| Percentages | Refer to the corresponding dimension of the content area |
| Animation | If the list lengths match, by computed value type per item; discrete otherwise |
| Support | Chrome 57 · Firefox 70 · Safari 10.1 |
| Spec | css-grid-2 |
grid-auto-rows: minmax(8rem, auto);
grid-auto-flow
How items without an explicit position are placed: filling each row in turn (row) or each column (column). dense lets later items backfill holes left earlier, which can put items out of source order.
| Syntax | [ row | column ] || dense |
|---|---|
| Initial | row |
| Applies to | Grid containers |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 57 · Firefox 52 · Safari 10.1 |
| Spec | css-grid-2 |
Placement
Placement properties take <grid-line> values: auto; a line number, where negative numbers count back from the end of the explicit grid; a line name, optionally with a number to pick the nth line of that name; or span with a number or name, to cover that many tracks or reach the next line of that name.
grid-area
Shorthand for all four placement longhands, in the order row-start / column-start / row-end / column-end. A single name places the item in the matching area from grid-template-areas.
| Syntax | <grid-line> [ / <grid-line> ]{0,3} |
|---|---|
| Initial | auto |
| Applies to | Grid items and absolutely positioned boxes whose containing block is a grid container |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 57 · Firefox 52 · Safari 10.1 |
| Spec | css-grid-2 |
main { grid-area: main; }
.banner { grid-area: 1 / 1 / 2 / -1; } /* first row, every column */
grid-row, grid-column
Shorthands for the start and end lines in one axis, separated by a slash. With one value the end is auto, which spans a single track, unless the value is a name, which sets both ends to that name.
| Syntax | <grid-line> [ / <grid-line> ]? |
|---|---|
| Initial | auto |
| Applies to | Grid items and absolutely positioned boxes whose containing block is a grid container |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 57 · Firefox 52 · Safari 10.1 |
| Spec | css-grid-2 |
grid-column: 2 / span 3;
grid-row-start, grid-row-end, grid-column-start, grid-column-end
The grid line where an item starts or ends in one axis. An item with auto on both ends of an axis is placed by the auto-placement algorithm.
| Syntax | <grid-line> |
|---|---|
| Initial | auto |
| Applies to | Grid items and absolutely positioned boxes whose containing block is a grid container |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 57 · Firefox 52 · Safari 10.1 |
| Spec | css-grid-2 |
Alignment
In a grid, justify-* works along the inline (row) axis and align-* along the block (column) axis. The place-* shorthands take the align-* value first and the justify-* value second; one value sets both. The value groups such as <self-position> are explained under Flexbox alignment.
justify-items
Aligns every item within its grid area along the inline axis, by setting the default that each child's justify-self: auto picks up. Flex containers ignore it.
| Syntax | normal | stretch | <baseline-position> | <overflow-position>? [ <self-position> | left | right ] | legacy | legacy && [ left | right | center ] |
|---|---|
| Initial | legacy |
| Applies to | All elements |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 52 · Firefox 20 · Safari 9 Block containers: Chrome 130 · Firefox no · Safari no |
| Spec | css-align-3 |
On its own, legacy computes to normal. If the parent's value is legacy with left, right or center, the element takes that value instead, so the alignment passes down the tree.
justify-self
Aligns one item within its grid area along the inline axis, overriding justify-items. auto uses the parent's justify-items. It does not apply to flex items; use an auto margin there.
| Syntax | auto | <overflow-position>? [ normal | <self-position> | left | right ] | stretch | <baseline-position> | anchor-center |
|---|---|
| Initial | auto |
| Applies to | Block-level boxes, absolutely positioned boxes and grid items |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 57 · Firefox 45 · Safari 10.1 Block-level boxes: Chrome 130 · Firefox no · Safari no Absolutely positioned boxes: Chrome 122 · Firefox 134 · Safari 26 anchor-center: Chrome 125 · Firefox 147 · Safari 26 |
| Spec | css-align-3 |
place-content
Shorthand for align-content and justify-content: where the grid's tracks, or a flex container's lines and items, sit within the container.
| Syntax | <'align-content'> <'justify-content'>? |
|---|---|
| Initial | normal |
| Applies to | See individual properties |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 59 · Firefox 45 · Safari 9 |
| Spec | css-align-3 |
place-items
Shorthand for align-items and justify-items: the default alignment of every item in its area.
| Syntax | <'align-items'> <'justify-items'>? |
|---|---|
| Initial | See individual properties |
| Applies to | All elements |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 59 · Firefox 45 · Safari 11 |
| Spec | css-align-3 |
.stage { display: grid; place-items: center; }
place-self
Shorthand for align-self and justify-self: the alignment of one item in its area.
| Syntax | <'align-self'> <'justify-self'>? |
|---|---|
| Initial | auto |
| Applies to | See individual properties |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 59 · Firefox 45 · Safari 11 |
| Spec | css-align-3 |