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'>
Initialnone
Applies toGrid containers
InheritedSee individual properties
PercentagesSee individual properties
AnimationSee individual properties
SupportChrome 57 · Firefox 52 · Safari 10.1
Speccss-grid-2
grid: auto-flow / repeat(3, 1fr);
Three equal columns; rows are created as items are placed.

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.

Syntaxnone | [ <'grid-template-rows'> / <'grid-template-columns'> ] | [ <line-names>? <string> <track-size>? <line-names>? ]+ [ / <explicit-track-list> ]?
Initialnone
Applies toGrid containers
InheritedSee individual properties
PercentagesSee individual properties
AnimationSee individual properties
SupportChrome 57 · Firefox 52 · Safari 10.1
Speccss-grid-2
grid-template:
  "head head" auto
  "side main" 1fr
  / 12rem 1fr;
A header across the top, a 12rem sidebar and a main area that takes the rest.

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.

Syntaxnone | <track-list> | <auto-track-list> | subgrid <line-name-list>?
Initialnone
Applies toGrid containers
InheritedNo
PercentagesRefer to the corresponding dimension of the content area
AnimationIf the list lengths match, by computed value type per item in the computed track list; discrete otherwise
SupportChrome 57 · Firefox 52 · Safari 10.1
subgrid: Chrome 117 · Firefox 71 · Safari 16
Speccss-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));
As many columns of at least 12rem as fit, sharing out the leftover width.

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.

Syntaxnone | <string>+
Initialnone
Applies toGrid containers
InheritedNo
AnimationDiscrete
SupportChrome 57 · Firefox 52 · Safari 10.1
Speccss-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>+
Initialauto
Applies toGrid containers
InheritedNo
PercentagesRefer to the corresponding dimension of the content area
AnimationIf the list lengths match, by computed value type per item; discrete otherwise
SupportChrome 57 · Firefox 70 · Safari 10.1
Speccss-grid-2
grid-auto-rows: minmax(8rem, auto);
Every new row is at least 8rem tall and grows to fit its content.

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
Initialrow
Applies toGrid containers
InheritedNo
AnimationDiscrete
SupportChrome 57 · Firefox 52 · Safari 10.1
Speccss-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}
Initialauto
Applies toGrid items and absolutely positioned boxes whose containing block is a grid container
InheritedNo
AnimationDiscrete
SupportChrome 57 · Firefox 52 · Safari 10.1
Speccss-grid-2
main { grid-area: main; }
.banner { grid-area: 1 / 1 / 2 / -1; }  /* first row, every column */
Placing items by area name and by line numbers.

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> ]?
Initialauto
Applies toGrid items and absolutely positioned boxes whose containing block is a grid container
InheritedNo
AnimationDiscrete
SupportChrome 57 · Firefox 52 · Safari 10.1
Speccss-grid-2
grid-column: 2 / span 3;
Starts at column line 2 and covers three columns.

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>
Initialauto
Applies toGrid items and absolutely positioned boxes whose containing block is a grid container
InheritedNo
AnimationDiscrete
SupportChrome 57 · Firefox 52 · Safari 10.1
Speccss-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.

Syntaxnormal | stretch | <baseline-position> | <overflow-position>? [ <self-position> | left | right ] | legacy | legacy && [ left | right | center ]
Initiallegacy
Applies toAll elements
InheritedNo
AnimationDiscrete
SupportChrome 52 · Firefox 20 · Safari 9
Block containers: Chrome 130 · Firefox no · Safari no
Speccss-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.

Syntaxauto | <overflow-position>? [ normal | <self-position> | left | right ] | stretch | <baseline-position> | anchor-center
Initialauto
Applies toBlock-level boxes, absolutely positioned boxes and grid items
InheritedNo
AnimationDiscrete
SupportChrome 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
Speccss-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'>?
Initialnormal
Applies toSee individual properties
InheritedNo
AnimationDiscrete
SupportChrome 59 · Firefox 45 · Safari 9
Speccss-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'>?
InitialSee individual properties
Applies toAll elements
InheritedNo
AnimationDiscrete
SupportChrome 59 · Firefox 45 · Safari 11
Speccss-align-3
.stage { display: grid; place-items: center; }
Centres the content both ways.

place-self

Shorthand for align-self and justify-self: the alignment of one item in its area.

Syntax<'align-self'> <'justify-self'>?
Initialauto
Applies toSee individual properties
InheritedNo
AnimationDiscrete
SupportChrome 59 · Firefox 45 · Safari 11
Speccss-align-3