CSS Reference ยท Grid
The grid property
Reference for the CSS grid shorthand: how it sets grid-template-* and grid-auto-* in one declaration, the three syntax forms, what it resets, and examples.
The grid property is a shorthand that sets all the explicit and implicit grid properties in one declaration: grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns and grid-auto-flow.
It does not turn an element into a grid; you still need display: grid. And because it resets six properties at once, many developers use it only for small cases and write the longhands for anything complicated.
.page { display: grid; grid: auto 1fr auto / 220px 1fr; } .feed { display: grid; grid: auto-flow 80px / repeat(3, 1fr); }
Try it
The first box defines two rows and three columns; the fifth item fills the second row's second cell, and any further items would form new rows of automatic height. The second box defines only the columns and says that new rows are created automatically at 40px each.
Syntax
grid: none
| <'grid-template'>
| <'grid-template-rows'> / [ auto-flow && dense? ] <'grid-auto-columns'>?
| [ auto-flow && dense? ] <'grid-auto-rows'>? / <'grid-template-columns'>;
- Initial value:
none(every longhand gets its initial value) - Applies to: grid containers
- Inherited: no
- Animatable: the track lists can animate when their structure matches; the other parts cannot
- Computed value: as specified for each longhand
Values
| Form | Description |
|---|---|
none |
Resets all six longhands to their initial values. |
<rows> / <columns> |
Same as grid-template: explicit tracks, or "areas" strings with row sizes. |
<rows> / auto-flow [dense] <auto-columns> |
Rows are explicit; columns are generated as items need them (grid-auto-flow: column). |
auto-flow [dense] <auto-rows> / <columns> |
Columns are explicit; rows are generated automatically (grid-auto-flow: row). |
Usage notes
- It resets a lot. Any longhand you don't mention goes back to its initial value. Place
gridbefore the more specific longhands in your CSS. - Gaps are not reset.
gap,row-gapandcolumn-gapare separate properties and stay unchanged. - One axis is automatic. In the
auto-flowforms you can't also give explicit tracks for the auto axis; only the auto-sized tracks are set. - Readability. The slash separates rows from columns in every form. If a declaration is hard to read, split it into longhands.
- Named lines and areas. The areas form can only be used in the
grid-templatebranch; theauto-flowbranches can't setgrid-template-areas.
Browser support
Supported in all current browsers.
Related
grid-templateโ the explicit part of the shorthandgrid-auto-flowโ controls auto-placementgrid-template-columns- Lessons: Grid, Grid Placement