Skip to content
Go to file
Cannot retrieve contributors at this time
5710 lines (4856 sloc) 249 KB
<pre class='metadata'>
Title: CSS Grid Layout Module Level 1
Status: ED
Work Status: Testing
Shortname: css-grid
Level: 1
Group: csswg
Previous version:
Previous version:
Previous version:
Previous version:
Previous version:
Previous version:
Previous version:
Previous version:
Previous version:
Previous version:
Previous version:
Previous version:
Previous version:
Previous version:
Previous version:
Previous version:
Editor: Tab Atkins Jr., Google,, w3cid 42199
Editor: Elika J. Etemad / fantasai, Invited Expert,, w3cid 35400
Editor: Rossen Atanassov, Microsoft,, w3cid 49885
Editor: Oriol Brufau, Igalia,, w3cid 108473
Former editor: Alex Mogilevsky, Microsoft Corporation,
Former editor: Phil Cupp, Microsoft Corporation,
Issue Tracking: Disposition of Comments
Abstract: This CSS module defines a two-dimensional grid-based layout system, optimized for user interface design. In the grid layout model, the children of a grid container can be positioned into arbitrary slots in a predefined flexible or fixed-size layout grid.
Ignored Terms: auto, grid-*-start, grid-*-end, flex factor, flex factors, grid-auto-position
Ignored Vars: A, B, C, size-contribution, track-sizes, extra-space
Link Defaults: css2 (property) margin/min-height/max-height/min-width/max-width, css-align-3 (value) stretch/baseline, css-position-3 (property) left, css-position-3 (property) position, css-writing-modes-3 (dfn) start/end
At Risk: application of grid placement to absolutely-positioned boxes
WPT Path Prefix: css/css-grid/
<pre class="link-defaults">
text:stretch; for:align-self
text:stretch; for:align-content
text:space-between; for:justify-content
text:space-around; for:justify-content
type:property; text:column-gap
type:dfn; text:alignment baseline
spec:css-break-3; type:dfn; text:fragment
text: specified size
text: content size
text: transferred size
spec:css-pseudo-4; type:selector;
.example {
.pseudo-code {
.pseudo-code > ol {
.pseudo-code > ol > li > ol {
.pseudo-code > ol > li > ol > li > ol {
.pseudo-code ul {
dd > p:nth-child(1) {
dd:last-child {
margin-bottom: 0;
dl.phase {
padding-left: .5em;
border-left: .5em #e0e0e0 solid;
#grid-property-breakdown tr:nth-child(3) td {
width: 25%;
border-style: solid;
Issue: If you notice any inconsistencies between this Grid Layout Module
and the <a href="">Flexible Box Layout Module</a>,
please report them to the CSSWG,
as this is likely an error.
<h2 id='intro'>
<em>This section is not normative.</em>
Grid Layout is a new layout model for CSS
that has powerful abilities to control the sizing and positioning of boxes and their contents.
Unlike <a href="">Flexible Box Layout</a>, which is single-axis&ndash;oriented,
Grid Layout is optimized for 2-dimensional layouts:
those in which alignment of content is desired in both dimensions.
<img src="images/flex-layout.png"
alt="An example of flex layout:
two rows of items,
the first being three items a third of the space each,
and the second being five items, a fifth of the space each.
There is therefore alignment along the “rows”, but not along the “columns”.">
<figcaption>Representative Flex Layout Example</figcaption>
<img src="images/grid-layout.png"
alt="An example of grid layout:
two rows of items,
the first being four items&mdash;the last of which spans both rows,
and the second being two items&mdash;the first of which spans the first two columns&mdash; plus the spanned item from the first row.">
<figcaption>Representative Grid Layout Example</figcaption>
In addition, due to its ability to explicitly position items in the grid,
Grid Layout allows dramatic transformations in visual layout structure
without requiring corresponding markup changes.
By combining <a href="">media queries</a> with the CSS properties that control layout of the grid container and its children,
authors can adapt their layout to changes in device form factors, orientation, and available space,
while preserving a more ideal semantic structuring of their content
across presentations.
Although many layouts can be expressed with either Grid or Flexbox,
they each have their specialties.
Grid enforces 2-dimensional alignment,
uses a top-down approach to layout,
allows explicit overlapping of items,
and has more powerful spanning capabilities.
Flexbox focuses on space distribution within an axis,
uses a simpler bottom-up approach to layout,
can use a content-size&ndash;based line-wrapping system to control its secondary axis,
and relies on the underlying markup hierarchy
to build more complex layouts.
It is expected that both will be valuable
and complementary tools for CSS authors.
<h3 id='background'>
Background and Motivation</h3>
<figure class="sidefigure">
<img class="figure" alt="Image: Application layout example requiring horizontal and vertical alignment." src="images/basic-form.png" />
<figcaption>Application layout example requiring horizontal and vertical alignment.</figcaption>
As websites evolved from simple documents into complex, interactive applications,
techniques for document layout, e.g. floats,
were not necessarily well suited for application layout.
By using a combination of tables, JavaScript, or careful measurements on floated elements,
authors discovered workarounds to achieve desired layouts.
Layouts that adapted to the available space were often brittle
and resulted in counter-intuitive behavior as space became constrained.
As an alternative, authors of many web applications opted for a fixed layout
that cannot take advantage of changes in the available rendering space on a screen.
The capabilities of grid layout address these problems.
It provides a mechanism for authors to divide available space for layout into columns and rows
using a set of predictable sizing behaviors.
Authors can then precisely position and size the building block elements of their application
into the <a>grid areas</a> defined by the intersections of these columns and rows.
The following examples illustrate the adaptive capabilities of grid layout,
and how it allows a cleaner separation of content and style.
<h4 id='adapting-to-available-space'>
Adapting Layouts to Available Space</h4>
<figure class="sidefigure">
<img src="images/game-smaller.png"
alt="Let us consider the layout of a game in two columns and three rows: the game title in the top left corner, the menu below it, and the score in the bottom left with the game board occupying the top and middle cells on the right followed by game controls filling the bottom left. The left column is sized to exactly fit its contents (the game title, menu items, and score), with the right column filling the remaining space."/>
<figcaption>Five grid items arranged according to content size and available space.</figcaption>
<figure class="sidefigure">
<img src="images/game-larger.png"
alt="As more space becomes available in larger screens, the middle row / right column are allowed to expand to fill that space." />
<figcaption>Growth in the grid due to an increase in available space.</figcaption>
Grid layout can be used to intelligently resize elements within a webpage.
The adjacent figures represent a game with five major components in the layout:
the game title, stats area, game board, score area, and control area.
The author's intent is to divide the space for the game such that:
The stats area always appears immediately under the game title.
The game board appears to the right of the stats and title.
The top of the game title and the game board should always align.
The bottom of the game board and bottom of the stats area align when the game has reached its minimum height.
In all other cases the game board will stretch to take advantage of all the space available to it.
The controls are centered under the game board.
The top of the score area is aligned to the top of the controls area.
The score area is beneath the stats area.
The score area is aligned to the controls beneath the stats area.
The following grid layout example shows how an author might achieve
all the sizing, placement, and alignment rules declaratively.
<div class="example">
<pre class="lang-css">
* Define the space for each <a>grid item</a> by declaring the grid
* on the <a>grid container</a>.
#grid {
* Two columns:
* 1. the first sized to content,
* 2. the second receives the remaining space
* (but is never smaller than the minimum size of the board
* or the game controls, which occupy this column [Figure 4])
* Three rows:
* 3. the first sized to content,
* 4. the middle row receives the remaining space
* (but is never smaller than the minimum height
* of the board or stats areas)
* 5. the last sized to content.
display: grid;
/* 1 */ auto
/* 2 */ 1fr;
/* 3 */ auto
/* 4 */ 1fr
/* 5 */ auto;
/* Specify the position of each <a>grid item</a> using coordinates on
* the 'grid-row' and 'grid-column' properties of each <a>grid item</a>.
#title { grid-column: 1; grid-row: 1; }
#score { grid-column: 1; grid-row: 3; }
#stats { grid-column: 1; grid-row: 2; align-self: start; }
#board { grid-column: 2; grid-row: 1 / span 2; }
#controls { grid-column: 2; grid-row: 3; justify-self: center; }
<pre class="lang-markup">
&lt;div id="grid">
&lt;div id="title">Game Title&lt;/div>
&lt;div id="score">Score&lt;/div>
&lt;div id="stats">Stats&lt;/div>
&lt;div id="board">Board&lt;/div>
&lt;div id="controls">Controls&lt;/div>
Note: There are multiple ways to specify the structure of the grid
and to position and size <a>grid items</a>,
each optimized for different scenarios.
<h4 id='source-independence'>
Source-Order Independence</h4>
<figure class="sidefigure">
<img alt="Image: An arrangement suitable for portrait orientation." src="images/game-portrait.png" />
<figcaption>An arrangement suitable for “portrait” orientation.</figcaption>
<figure class="sidefigure">
<img alt="Image: An arrangement suitable for landscape orientation." src="images/game-landscape.png" />
<figcaption>An arrangement suitable for “landscape“ orientation.</figcaption>
Continuing the prior example,
the author also wants the game to adapt to different devices.
Also, the game should optimize the placement of the components when viewed either in portrait or landscape orientation (Figures 6 and 7).
By combining grid layout with media queries,
the author is able to use the same semantic markup,
but rearrange the layout of elements independent of their source order,
to achieve the desired layout in both orientations.
The following example uses grid layout’s ability to name the space which will be occupied by a <a>grid item</a>.
This allows the author to avoid rewriting rules for <a>grid items</a>
as the grid’s definition changes.
<div class="example">
<pre class="lang-css">
@media (orientation: portrait) {
#grid {
display: grid;
/* The rows, columns and areas of the grid are defined visually
* using the grid-template-areas property. Each string is a row,
* and each word an area. The number of words in a string
* determines the number of columns. Note the number of words
* in each string must be identical. */
grid-template-areas: "title stats"
"score stats"
"board board"
"ctrls ctrls";
/* The way to size columns and rows can be assigned with the
* grid-template-columns and grid-template-rows properties. */
grid-template-columns: auto 1fr;
grid-template-rows: auto auto 1fr auto;
@media (orientation: landscape) {
#grid {
display: grid;
/* Again the template property defines areas of the same name,
* but this time positioned differently to better suit a
* landscape orientation. */
grid-template-areas: "title board"
"stats board"
"score ctrls";
grid-template-columns: auto 1fr;
grid-template-rows: auto 1fr auto;
/* The grid-area property places a grid item into a named
* area of the grid. */
#title { grid-area: title }
#score { grid-area: score }
#stats { grid-area: stats }
#board { grid-area: board }
#controls { grid-area: ctrls }
<pre class="lang-markup">
&lt;div id="grid">
&lt;div id="title">Game Title&lt;/div>
&lt;div id="score">Score&lt;/div>
&lt;div id="stats">Stats&lt;/div>
&lt;div id="board">Board&lt;/div>
&lt;div id="controls">Controls&lt;/div>
Note: The reordering capabilities of grid layout intentionally affect
<em>only the visual rendering</em>,
leaving speech order and navigation based on the source order.
This allows authors to manipulate the visual presentation
while leaving the source order intact and optimized for non-CSS UAs
and for linear models such as speech and sequential navigation.
Advisement: Grid item placement and reordering must not be used
as a substitute for correct source ordering,
as that can ruin the accessibility of the document.
<h3 id="values">
Value Definitions</h3>
This specification follows the <a href="">CSS property definition conventions</a> from [[!CSS2]]
using the <a href="">value definition syntax</a> from [[!CSS-VALUES-3]].
Value types not defined in this specification are defined in CSS Values &amp; Units [[!CSS-VALUES-3]].
Combination with other CSS modules may expand the definitions of these value types.
In addition to the property-specific values listed in their definitions,
all properties defined in this specification
also accept the <a>CSS-wide keywords</a> as their property value.
For readability they have not been repeated explicitly.
<h2 id='overview'>
<em>This section is not normative.</em>
Grid Layout controls the layout of its content
through the use of a <a>grid</a>:
an intersecting set of horizontal and vertical lines
which create a sizing and positioning coordinate system
for the <a>grid container</a>’s contents.
Grid Layout features
fixed, flexible, and content-based [[#track-sizing|track sizing functions]]
[[#placement|explicit item placement]] via forwards (positive) and backwards (negative) numerical grid coordinates,
named grid lines, and named grid areas;
automatic item placement into empty areas, including <a section href="#order-property">reordering with <css>order</css></a>
space-sensitive track repetition
automatic addition of rows or columns to accommodate additional content
control over alignment and spacing with
[[#auto-margins|margins]], <a>gutters</a>, and the <a href="">alignment properties</a>
the ability to overlap content and <a section href="#z-order">control layering with <css>z-index</css></a>
<a>Grid containers</a> can be nested or mixed with <a>flex containers</a>
as necessary to create more complex layouts.
<h3 id="overview-grid">
Declaring the Grid</h3>
The <a>tracks</a> (<a>rows</a> and <a>columns</a>) of the <a>grid</a>
are declared and sized
either explicitly through the <a>explicit grid</a> properties
or are implicitly created when items are placed outside the <a>explicit grid</a>.
The 'grid' shorthand and its sub-properties define the parameters
of the grid.
<div class="example">
Below are some examples of grid declarations:
The following declares a grid with four named areas:
<code>H</code>, <code>A</code>, <code>B</code>,
and <code>F</code>.
The first column is sized to fit its contents (''grid-template-columns/auto''),
and the second column takes up the remaining space (<a value lt=fr>1fr</a>).
Rows default to ''grid-template-rows/auto'' (content-based) sizing;
the last row is given a fixed size of ''30px''.
main {
grid: "H H "
"A B "
"F F " 30px
/ auto 1fr;
The following declares a grid with as many rows of at least ''5em''
as will fit in the height of the grid container (''100vh'').
The grid has no explicit columns;
instead columns are added as content is added,
the resulting column widths are equalized (<a value lt=fr>1fr</a>).
Since content overflowing to the right won't print,
an alternate layout for printing adds rows instead.
main {
grid: repeat(auto-fill, 5em) / auto-flow 1fr;
height: 100vh;
@media print {
main {
grid: auto-flow 1fr / repeat(auto-fill, 5em);
The following declares a grid with 5 evenly-sized columns
and three rows,
with the middle row taking up all remaining space
(and at least enough to fit its contents).
main {
grid: auto 1fr auto / repeat(5, 1fr);
min-height: 100vh;
<h3 id="overview-placement">
Placing Items</h3>
The contents of the <a>grid container</a> are organized into individual <a>grid items</a>
(analogous to <a>flex items</a>),
which are then assigned to predefined [=grid areas|areas=] in the <a>grid</a>.
They can be explicitly placed using coordinates through the <a>grid-placement properties</a>
or implicitly placed into empty areas using [=auto-placement=].
<div class="example">
Below are some examples of grid placement declarations
using the 'grid-area' shorthand:
grid-area: a; /* Place into named grid area “a” */
grid-area: auto; /* Auto-place into next empty area */
grid-area: 2 / 4; /* Place into row 2, column 4 */
grid-area: 1 / 3 / -1; /* Place into column 3, span all rows */
grid-area: header-start / sidebar-start / footer-end / sidebar-end;
/* Place using named lines */
These are equivalent to the following 'grid-row' + 'grid-column' declarations:
grid-row: a; grid-column: a;
grid-row: auto; grid-column: auto;
grid-row: 2; grid-column: 4;
grid-row: 1 / -1; grid-column: 3;
grid-row: header-start / footer-end; grid-column: sidebar-start / sidebar-end;
They can further be decomposed into the 'grid-row-start'/'grid-row-end'/'grid-column-start'/'grid-column-end' longhands, e.g.
grid-area: a;
/* Equivalent to grid-row-start: a; grid-column-start: a; grid-row-end: a; grid-column-end: a; */
grid-area: 1 / 3 / -1;
/* Equivalent to grid-row-start: 1; grid-column-start: 3; grid-row-end: -1; grid-column-end: auto; */
<h3 id="overview-sizing">
Sizing the Grid</h3>
Once the <a>grid items</a> have been [[#placement|placed]],
the sizes of the <a>grid tracks</a> (rows and columns) are calculated,
accounting for the sizes of their contents and/or available space as specified in the grid definition.
The resulting sized grid is [[#grid-align|aligned]] within the <a>grid container</a>
according to the <a>grid container</a>’s 'align-content' and 'justify-content' properties.
<div class="example">
The following example justifies all columns
by distributing any extra space among them,
and centers the grid in the <a>grid container</a>
when it is smaller than 100vh.
main {
grid: auto-flow 1fr / repeat(auto-fill, 5em);
min-height: 100vh;
justify-content: space-between;
align-content: safe center;
Finally each <a>grid item</a> is sized and aligned within its assigned <a>grid area</a>,
as specified by its own <a href="">sizing</a> [[!CSS2]] and <a lt="box alignment properties">alignment properties</a> [[!CSS-ALIGN-3]].
██████ ███████ ██ ██ ██████ ████████ ████████ ████████ ██████
██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██████ ████████ ██ ██████
██ ██ ██ ██ ████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██
██████ ███████ ██ ██ ██████ ████████ ██ ██ ██████
<h2 id='grid-concepts'>
Grid Layout Concepts and Terminology</h2>
In <dfn export>grid layout</dfn>,
the content of a <a>grid container</a> is laid out
by positioning and aligning it into a <a>grid</a>.
The <dfn export>grid</dfn> is an intersecting set of horizontal and vertical <a>grid lines</a>
that divides the <a>grid container</a>’s space into <a>grid areas</a>,
into which <a>grid items</a> (representing the <a>grid container</a>’s content) can be placed.
There are two sets of <a>grid lines</a>:
one set defining <dfn export lt="grid column" local-lt="column">columns</dfn>
that run along the <a href="">block axis</a>,
and an orthogonal set defining <dfn export lt="grid row" local-lt="row">rows</dfn>
along the <a href="">inline axis</a>.
<div class="figure">
<img class="figure" alt="Image: A diagram illustrating the relationship between the Grid Element and its Tracks, Lines, Areas and Items." src="images/grid-concepts.png" />
<p class="caption">A diagram illustrating the relationship between the <a>grid container</a> and its tracks, lines, areas and items.
<img class="figure" alt="Image: Grid Lines." src="images/grid-lines.png" />
<figcaption>Grid lines: Three in the block axis and four in the inline axis.</figcaption>
<h3 id="grid-line-concept">
Grid Lines</h3>
<dfn id='grid-line' export lt='grid line|grid row line|grid column line'>Grid lines</dfn> are the horizontal and vertical dividing lines of the <a>grid</a>.
A <a>grid line</a> exists on either side of a column or row.
They can be referred to by numerical index,
or by an author-specified name.
A <a>grid item</a> references the <a>grid lines</a> to determine its position within the <a>grid</a>
using the [[#placement|grid-placement properties]].
<div class="example">
The following two examples both create three column <a>grid lines</a> and four row <a>grid lines</a>.
This first example demonstrates how an author would position a <a>grid item</a> using <a>grid line</a> numbers:
<pre class="lang-css">
#grid {
display: grid;
grid-template-columns: 150px 1fr;
grid-template-rows: 50px 1fr 50px;
#item1 { grid-column: 2;
grid-row-start: 1; grid-row-end: 4; }
This second example uses explicitly named <a>grid lines</a>:
<pre class="lang-css">
/* equivalent layout to the prior example, but using named lines */
#grid {
display: grid;
grid-template-columns: 150px [item1-start] 1fr [item1-end];
grid-template-rows: [item1-start] 50px 1fr 50px [item1-end];
#item1 {
grid-column: item1-start / item1-end;
grid-row: item1-start / item1-end;
<h3 id="grid-track-concept">
Grid Tracks and Cells</h3>
<dfn export local-lt="track">Grid track</dfn> is a generic term for a <a>grid column</a> or <a>grid row</a>&mdash;in
other words, it is the space between two adjacent <a>grid lines</a>.
Each <a>grid track</a> is assigned a sizing function,
which controls how wide or tall the column or row may grow,
and thus how far apart its bounding <a>grid lines</a> are.
Adjacent <a>grid tracks</a> can be separated by [[#gutters|gutters]]
but are otherwise packed tightly.
A <dfn export>grid cell</dfn> is the intersection of a grid row and a grid column.
It is the smallest unit of the grid that can be referenced when positioning <a>grid items</a>.
<div class="example">
In the following example there are two columns and three rows.
The first column is fixed at 150px.
The second column uses flexible sizing, which is a function of the unassigned space in the grid,
and thus will vary as the width of the <a>grid container</a> changes.
If the used width of the <a>grid container</a> is 200px, then the second column is 50px wide.
If the used width of the <a>grid container</a> is 100px, then the second column is 0px
and any content positioned in the column will overflow the <a>grid container</a>.
#grid {
display: grid;
grid-template-columns: 150px 1fr; /* two columns */
grid-template-rows: 50px 1fr 50px; /* three rows */
<h3 id="grid-area-concept">
Grid Areas</h3>
A <dfn export>grid area</dfn> is the logical space used to lay out one or more <a>grid items</a>.
A <a>grid area</a> consists of one or more adjacent <a>grid cells</a>.
It is bound by four <a>grid lines</a>, one on each side of the <a>grid area</a>,
and participates in the sizing of the <a>grid tracks</a> it intersects.
A <a>grid area</a> can be named explicitly using the 'grid-template-areas' property of the <a>grid container</a>,
or referenced implicitly by its bounding <a>grid lines</a>.
A <a>grid item</a> is assigned to a <a>grid area</a>
using the [[#placement|grid-placement properties]].
<div class="example">
/* using the template syntax */
#grid {
display: grid;
grid-template-areas: ". a"
"b a"
". a";
grid-template-columns: 150px 1fr;
grid-template-rows: 50px 1fr 50px;
#item1 { grid-area: a }
#item2 { grid-area: b }
#item3 { grid-area: b }
/* Align items 2 and 3 at different points in the grid area "b". */
/* By default, grid items are stretched to fit their grid area */
/* and these items would layer one over the other. */
#item2 { align-self: start; }
#item3 { justify-self: end; align-self: end; }
A <a>grid item</a>’s <a>grid area</a> forms the containing block into which it is laid out.
<a>Grid items</a> placed into the same <a>grid area</a> do not directly affect each other's layout.
Indirectly, however, a <a>grid item</a> occupying a <a>grid track</a> with an <a>intrinsic sizing function</a>
can affect the size of that track (and thus the positions of its bounding <a>grid lines</a>),
which in turn can affect the position or size of another <a>grid item</a>.
<h2 id="order-accessibility">
Reordering and Accessibility</h2>
Grid layout gives authors great powers of rearrangement over the document.
However, these are not a substitute for correct ordering of the document source.
The 'order' property and <a>grid placement</a>
<em>do not</em> affect ordering in non-visual media
(such as <a href="">speech</a>).
Likewise, rearranging grid items visually does not affect
the default traversal order of sequential navigation modes
(such as cycling through links, see e.g. <a href=""><code>tabindex</code></a> [[HTML]]).
Authors <em>must</em> use 'order' and the <a>grid-placement properties</a>
only for visual, not logical, reordering of content.
Style sheets that use these features to perform logical reordering are non-conforming.
Note: This is so that non-visual media and non-CSS UAs,
which typically present content linearly,
can rely on a logical source order,
while grid layout’s placement and ordering features are used to tailor the visual arrangement.
(Since visual perception is two-dimensional and non-linear,
the desired visual order is not always equivalent to the desired reading order.)
<div class='example'>
Many web pages have a similar shape in the markup,
with a header on top,
a footer on bottom,
and then a content area and one or two additional columns in the middle.
it's desirable that the content come first in the page's source code,
before the additional columns.
However, this makes many common designs,
such as simply having the additional columns on the left and the content area on the right,
difficult to achieve.
This has been addressed in many ways over the years,
often going by the name "Holy Grail Layout" when there are two additional columns.
Grid Layout makes this example trivial.
For example, take the following sketch of a page's code and desired layout:
<div class='code-and-figure'>
<pre class="lang-markup">
&lt;!DOCTYPE html>
<div><img src="images/grid-order-page.svg" width=400 height=360 alt="In this page the header is at the top and the footer at the bottom, but the article is in the center, flanked by the nav on the right and the aside on the left."></div>
This layout can be easily achieved with grid layout:
<pre class="lang-css">
body { display: grid;
grid: "h h h"
"a b c"
"f f f";
grid-template-columns: auto 1fr 20%; }
article { grid-area: b; min-width: 12em; }
nav { grid-area: a; /* auto min-width */ }
aside { grid-area: c; min-width: 12em; }
As an added bonus,
the columns will all be <a value for="align-self" lt="stretch">equal-height</a> by default,
and the main content will be as wide as necessary to fill the screen.
this can then be combined with media queries to switch to an all-vertical layout on narrow screens:
<pre class="lang-css">
@media all and (max-width: 60em) {
/* Too narrow to support three columns */
main { display: block; }
In order to preserve the author's intended ordering in all presentation modes,
authoring tools&mdash;including WYSIWYG editors as well as Web-based authoring aids--
must reorder the underlying document source
and not use 'order' or <a>grid-placement properties</a> to perform reordering
unless the author has explicitly indicated that the underlying
document order (which determines speech and navigation order) should be
<em>out-of-sync</em> with the visual order.
<div class="example">
For example, a tool might offer both drag-and-drop arrangement of grid items
as well as handling of media queries for alternate layouts per screen size range.
Since most of the time, reordering should affect all screen ranges
as well as navigation and speech order,
the tool would match the resulting drag-and-drop visual arrangement
by simultaneously reordering the DOM layer.
In some cases, however, the author may want different visual arrangements per screen size.
The tool could offer this functionality
by using the <a>grid-placement properties</a> together with media queries,
but also tie the smallest screen size's arrangement to the underlying DOM order
(since this is most likely to be a logical linear presentation order)
while using <a>grid-placement properties</a>
to rearrange the visual presentation in other size ranges.
This tool would be conformant, whereas a tool that only ever used
the <a>grid-placement properties</a>
to handle drag-and-drop grid rearrangement
(however convenient it might be to implement it that way)
would be non-conformant.
████████ ████ ██████ ████████ ██ ███ ██ ██ ██ ██████ ████████ ████ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██████ ████████ ██ ██ ██ ██ ██ ████ ████████ ██ ██ ██
██ ██ ██ ██ ██ ██ █████████ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██
████████ ████ ██████ ██ ████████ ██ ██ ██ ██ ██████ ██ ██ ████ ████████
<h2 id="grid-model">
Grid Containers</h2>
<h3 id='grid-containers'>
Establishing Grid Containers: the ''display/grid'' and ''inline-grid'' 'display' values</h3>
<pre class="propdef partial">
Name: display
New values: grid | inline-grid
<dl dfn-for="display" dfn-type=value>
This value causes an element to generate a <a>grid container</a> box
that is <a>block-level</a> when placed in <a>flow layout</a>.
This value causes an element to generate an <a>grid container</a> box
that is <a>inline-level</a> when placed in <a>flow layout</a>.
A <dfn export>grid container</dfn> establishes
an [=independent formatting context|independent=] <dfn export>grid formatting context</dfn>
for its contents.
This is the same as establishing an independent [=block formatting context=],
except that grid layout is used instead of block layout:
floats do not intrude into the grid container,
and the grid container's margins do not collapse with the margins of its contents.
The contents of a <a>grid container</a> are laid out into a <a>grid</a>,
with <a>grid lines</a> forming the boundaries of each <a>grid items</a>’ containing block.
Grid containers are not block containers,
and so some properties that were designed with the assumption of block layout
don't apply in the context of grid layout.
In particular:
'float' and 'clear' have no effect on a <a>grid item</a>.
However, the 'float' property still affects the computed value of 'display' on children of a grid container,
as this occurs <em>before</em> <a>grid items</a> are determined.
'vertical-align' has no effect on a grid item.
the ''::first-line'' and ''::first-letter'' pseudo-elements do not apply to <a>grid containers</a>,
and <a>grid containers</a> do not contribute a first formatted line or first letter to their ancestors.
If an element's specified 'display' is ''inline-grid''
and the element is floated or absolutely positioned,
the computed value of 'display' is <a value>grid</a>.
The table in <a href="">CSS 2.1 Chapter 9.7</a> is thus amended
to contain an additional row,
with ''inline-grid'' in the "Specified Value" column
and ''grid'' in the "Computed Value" column.
<h3 id='intrinsic-sizes'>
Sizing Grid Containers</h3>
Note see [[!CSS-SIZING-3]] for a definition of the terms in this section.
A <a>grid container</a> is sized
using the rules of the formatting context in which it participates:
As a <a>block-level</a> box in a <a>block formatting context</a>,
it is sized like a <a>block box</a> that establishes a formatting context,
with an ''auto'' <a>inline size</a> calculated as for non-replaced block boxes.
As an inline-level box in an <a>inline formatting context</a>,
it is sized as an atomic inline-level box (such as an inline-block).
In both inline and block formatting contexts,
the <a>grid container</a>’s ''auto'' <a>block size</a> is its max-content size.
<span class="note">The block layout spec should probably define this,
but it isn't written yet.</span>
The <a>max-content size</a> (<a>min-content size</a>) of a <a>grid container</a> is
the sum of the <a>grid container’s</a> track sizes (including gutters)
in the appropriate axis,
when the grid is sized under a <a>max-content constraint</a> (<a>min-content constraint</a>).
<h3 id='overflow'>
Scrollable Grid Overflow</h3>
The 'overflow' property applies to <a>grid containers</a>.
Just as it is included in intrinsic sizing (see above),
the <a>grid</a> is also included
in a <a>grid container</a>’s <a>scrollable overflow region</a>.
Note: Beware the interaction with padding
when the <a>grid container</a> is a <a>scroll container</a>:
additional padding is defined to be added
to the <a>scrollable overflow rectangle</a>
as needed to enable ''place-content: end'' alignment of scrollable content.
See [[css-overflow-3#scrollable]]
██████ ██ ███ ██ ██ ████████ ████ ██ ██ ██████
██ ██ ██ ██ ██ ███ ███ ██ ██ ██ ███ ██ ██ ██
██ ██ ██ ██ ████ ████ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ███ ██ ████████ ██ ██ ██ ██ ██ ████
██ ██ █████████ ██ ██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██
██████ ████████ ██ ██ ██ ██ ██ ████ ██ ██ ██████
<h3 id="overlarge-grids">
Limiting Large Grids</h3>
Since memory is limited,
UAs may clamp the possible size of the <a>implicit grid</a>
to be within a UA-defined limit
(which should accommodate lines at in the range [-10000, 10000]),
dropping all lines outside that limit.
If a grid item is placed outside this limit,
its grid area must be <a>clamped</a> to within this limited grid.
To <dfn local-lt=clamp>clamp a grid area</dfn>:
* If the <a>grid area</a> would <a>span</a> outside the limited grid,
its span is clamped to the last line of the limited <a>grid</a>.
* If the <a>grid area</a> would be placed completely outside the limited grid,
its span must be truncated to 1
and the area repositioned into the last <a>grid track</a> on that side of the grid.
<div class='example'>
For example, if a UA only supported grids with at most 1000 tracks in each dimension,
the following placement properties:
<pre class="lang-css">
.grid-item {
grid-row: 500 / 1500;
grid-column: 2000 / 3000;
Would end up being equivalent to:
<pre class="lang-css">
.grid-item {
grid-row: 500 / 1001;
grid-column: 1000 / 1001;
██████ ████████ ████ ████████ ████ ████████ ████████ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ███ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██
██ ████ ████████ ██ ██ ██ ██ ██ ██████ ██ ███ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ████ ████████ ████ ██ ████████ ██ ██ ██████
<h2 id="grid-items">
Grid Items</h2>
Loosely speaking, the <dfn export id="grid-item" lt="grid item">grid items</dfn> of a <a>grid container</a>
are boxes representing its in-flow contents.
Each in-flow child of a <a>grid container</a>
becomes a <a>grid item</a>,
and each contiguous sequence of child <a>text runs</a>
is wrapped in an <a>anonymous</a> <a>block container</a> <a>grid item</a>.
However, if the entire sequence of child <a>text runs</a> contains only
<a href="">white space</a>
(i.e. characters that can be affected by the 'white-space' property)
it is instead not rendered (just as if its <a>text nodes</a> were ''display:none'').
<div class="example">
<p>Examples of grid items:
<pre class="lang-markup">
&lt;div style="display: grid">
&lt;!-- grid item: block child -->
&lt;div id="item1">block&lt;/div>
&lt;!-- grid item: floated element; floating is ignored -->
&lt;div id="item2" style="float: left;">float&lt;/div>
&lt;!-- grid item: anonymous block box around inline content -->
anonymous item 3
&lt;!-- grid item: inline child -->
item 4
&lt;!-- grid items do not <a href="">split</a> around blocks -->
&lt;q style="display: block" id=not-an-item>item 4&lt;/q>
item 4
<figcaption>grid items determined from above code block</figcaption>
<a href="examples/grid-item-determination.html">
<object type="image/png" data="images/grid-item-determination.png">
<li>grid item containing <samp>block</samp>.
<li>grid item containing <samp>float</samp>.
<li>(Anonymous, unstyleable) grid item containing <samp>anonymous item 3</samp>.
<li>grid item containing three blocks in succession:
<li>Anonymous block containing <samp>item 4</samp>.
<li><code>&lt;q></code> element block containing <samp>item 4</samp>.
<li>Anonymous block containing <samp>item 4</samp>.
Note: inter-element white space disappears:
it does not become its own grid item,
even though inter-element text <em>does</em> get wrapped in an anonymous grid item.
Note: The box of a anonymous item is unstyleable,
since there is no element to assign style rules to.
Its contents will however inherit styles (such as font settings) from the grid container.
<h3 id="grid-item-display">
Grid Item Display</h3>
A <a>grid item</a> [=establishes an independent formatting context=] for its contents.
However, grid items are <dfn export>grid-level</dfn> boxes, not block-level boxes:
they participate in their container's <a>grid formatting context</a>,
not in a block formatting context.
If the [=computed value|computed=] 'display' value of an element's nearest ancestor element
(skipping ''display:contents'' ancestors)
is ''grid'' or ''inline-grid'',
the element's own 'display' value is [=blockified=].
(See <a href="">CSS2.1&sect;9.7</a> [[!CSS2]]
and [[css-display-3#transformations]]
for details on this type of 'display' value conversion.)
Note: Blockification still occurs even when the ''grid'' or ''inline-grid'' element does not end up generating a [=grid container=] box,
e.g. when it is [=replaced element|replaced=]
or in a ''display: none'' subtree.
Note: Some values of 'display' normally trigger the creation of anonymous boxes around the original box.
If such a box is a <a>grid item</a>,
it is blockified first,
and so anonymous box creation will not happen.
For example, two contiguous <a>grid items</a> with ''display: table-cell''
will become two separate ''display: block'' <a>grid items</a>,
instead of being wrapped into a single anonymous table.
<h3 id="grid-item-sizing">
Grid Item Sizing</h3>
A <a>grid item</a> is sized within the containing block defined by its <a>grid area</a>.
Grid item calculations for 'auto' widths and heights
vary by their [[css-align-3#self-alignment|self-alignment values]]:
: ''align-self/normal''
If the grid item is a [=replaced element=] with an intrinsic size in the relevant dimension
(or with an aspect ratio and an intrinsic size in the other dimension)
the grid item is sized as for ''align-self: start''
(consistent with the width calculation rules for block-level replaced elements in [[CSS2/visudet#block-replaced-width]]).
use the [=inline size=] calculation rules for non-replaced boxes
(defined in [[CSS2/visudet#blockwidth]],
and modified by the rules for handling the 'aspect-ratio' property
when resolving ''width/auto'' sizes in [[CSS-SIZING-4]]).
: ''align-self/stretch''
Use the [=inline size=] calculation rules for non-replaced boxes
(defined in [[CSS2/visudet#blockwidth]],
and modified by the rules for handling the 'aspect-ratio' property
when resolving ''width/auto'' sizes in [[CSS-SIZING-4]]).
Note: This may distort the aspect ratio of the item, if it has one.
: all other values
:: Size the item as ''width/fit-content''.
<div class="note">
The following informative table summarizes the automatic sizing of grid items:
<table class=data>
<caption>Summary of automatic sizing behavior of grid items</caption>
<th>Non-replaced Element Size
<th>Replaced Element Size
<th scope=row>''align-self/normal''
<td>Fill grid area
<td>Use intrinsic size
<th scope=row>''align-self/stretch''
<td>Fill grid area
<td>Fill grid area
<th scope=row>''align-self/start''/<l spec=css-align-3>''align-self/center''</l>/etc.
<td>''width/fit-content'' sizing (like floats)
<td>Use intrinsic size
Note: The ''min-width/auto'' value of 'min-width' and 'min-height'
affects track sizing in the relevant axis
similar to how it affects the main size of a <a>flex item</a>.
See [[#min-size-auto]].
<h3 id="position-grid">
Non-children Grid Items</h3>
<p class="issue">
This is a proposal to create the ability to have descendants of a grid item participate in a grid layout,
similar to the behavior defined by the Template Layout module.
A descendant of the grid can be pulled out of flow and participate directly in the grid
by assigning it ''position: grid''.
An element with ''position: grid'' is pulled out of flow and participates as a grid item
belonging to the first ancestor with ''display: grid''.
If the element is positioned using named lines or slots,
it belongs to the first ancestor with ''display: grid'' that has all of the corresponding named lines/slots.
If no such ancestor exists, the item remains in flow.
<p class="issue">
Alternatively, the item can just go into the first grid,
and missing names are treated as <a value for="<grid-line>">auto</a>.
<h3 id='order-property'>
Reordered Grid Items: the 'order' property</h3>
The 'order' property also applies to <a>grid items</a>.
It affects their [[#grid-auto-flow-property|auto-placement]] and [[#z-order|painting order]].
Advisement: As with reordering flex items,
the 'order' property must only be used
when the visual order needs to be <em>out-of-sync</em>
with the speech and navigation order;
otherwise the underlying document source should be reordered instead.
See [[css-flexbox-1#order-accessibility]]
in [[CSS-FLEXBOX-1]].
<h3 id='item-margins'>
Grid Item Margins and Paddings</h3>
As adjacent grid items are independently contained
within the containing block formed by their <a>grid areas</a>,
the margins of adjacent <a>grid items</a> do not
<a href="">collapse</a>.
Percentage margins and paddings on <a>grid items</a>,
like those on <a>block boxes</a>,
are resolved against the <a>inline size</a> of their <a>containing block</a>,
e.g. left/right/top/bottom percentages
all resolve against their <a>containing block</a>’s <em>width</em>
in horizontal <a>writing modes</a>.
Auto margins expand to absorb extra space in the corresponding dimension,
and can therefore be used for alignment.
See [[#auto-margins]]
<h3 id='z-order'>
Z-axis Ordering: the 'z-index' property</h3>
<a>Grid items</a> can overlap when they are positioned into intersecting <a>grid areas</a>,
or even when positioned in non-intersecting areas because of negative margins or positioning.
The painting order of <a>grid items</a> is exactly the same as inline blocks [[CSS2]],
except that <a>order-modified document order</a> is used in place of raw document order,
and 'z-index' values other than <a value for=z-index>auto</a> create a stacking context even if 'position' is ''static''
(behaving exactly as if 'position' were ''relative'').
Thus the 'z-index' property can easily be used to control the z-axis order of grid items.
<p class='note'>
Note: Descendants that are positioned outside a grid item still participate in any stacking context established by the grid item.
<div class="example">
The following diagram shows several overlapping grid items,
with a combination of implicit source order
and explicit 'z-index'
used to control their stacking order.
<img src="images/drawing-order.png" />
<figcaption>Drawing order controlled by z-index and source order.</figcaption>
&lt;style type="text/css">
#grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr
#A { grid-column: 1 / span 2; grid-row: 2; align-self: end; }
#B { grid-column: 1; grid-row: 1; z-index: 10; }
#C { grid-column: 2; grid-row: 1; align-self: start; margin-left: -20px; }
#D { grid-column: 2; grid-row: 2; justify-self: end; align-self: start; }
#E { grid-column: 1 / span 2; grid-row: 1 / span 2;
z-index: 5; justify-self: center; align-self: center; }
&lt;div id="grid">
&lt;div id="A">A&lt;/div>
&lt;div id="B">B&lt;/div>
&lt;div id="C">C&lt;/div>
&lt;div id="D">D&lt;/div>
&lt;div id="E">E&lt;/div>
██ ██ ████ ██ ██
███ ███ ██ ███ ██ ██ ██
████ ████ ██ ████ ██ ██ ██
██ ███ ██ ██ ██ ██ ██ ███████ █████████
██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ███ ██ ██
██ ██ ████ ██ ██
<h3 id="min-size-auto">
Automatic Minimum Size of Grid Items</h3>
Note: Much of the sizing terminology used in this section
(and throughout the rest of the specification)
is defined in <a href="">CSS Intrinsic and Extrinsic Sizing</a> [[!CSS-SIZING-3]].
To provide a more reasonable default <a>minimum size</a> for <a>grid items</a>,
the used value of its <a>automatic minimum size</a> in a given axis
is the <a>content-based minimum size</a> if all of the following are true:
* it is not a [=scroll container=]
* it spans at least one [=track=] in that axis
whose <a>min track sizing function</a> is ''grid-template-rows/auto''
* if it spans more than one track in that axis, none of those tracks are [=flexible track|flexible=]
Otherwise, the [=automatic minimum size=] is zero, as usual.
The <dfn>content-based minimum size</dfn> for a <a>grid item</a> in a given dimension is
its <a>specified size suggestion</a> if it exists,
otherwise its <a>transferred size suggestion</a> if that exists,
else its <a>content size suggestion</a>,
see below.
to prevent the <a>content-based minimum size</a>
from forcing overflow of its fixed-size grid area,
if in a given dimension
the <a>grid item</a> spans only <a>grid tracks</a>
that have a <a lt="fixed sizing function">fixed</a> <a>max track sizing function</a>,
then its <a>specified size suggestion</a> and <a>content size suggestion</a> in that dimension
(and its input from this dimension
to the <a>transferred size suggestion</a> in the opposite dimension)
are further clamped to less than or equal to the <a>stretch fit</a>
into the <a>grid area</a>’s maximum size in that dimension,
as represented by the sum of those <a>grid tracks</a><a>max track sizing functions</a>
plus any intervening <a lt="fixed sizing function">fixed</a> <a>gutters</a>.
Note: The argument to ''fit-content()'' does <em>not</em> clamp
the [=content-based minimum size=] in the same way as a
<a lt="fixed sizing function">fixed</a> <a>max track sizing function</a>.
The <a>content size suggestion</a>, <a>specified size suggestion</a>, and <a>transferred size suggestion</a>
used in this calculation account for the relevant min/max/preferred size properties
so that the <a>content-based minimum size</a> does not interfere with any author-provided constraints,
and are defined below:
<dt><dfn>specified size suggestion</dfn>
If the item’s computed <a>preferred size property</a> in the relevant axis is <a>definite</a>,
then the <a>specified size suggestion</a> is that size
(clamped by the relevant <a>max size property</a> if it's <a>definite</a>).
It is otherwise undefined.
<dt><dfn>transferred size suggestion</dfn>
If the item has an intrinsic aspect ratio
and its computed <a>preferred size property</a> in the opposite axis is <a>definite</a>,
then the <a>transferred size suggestion</a> is that size
(clamped by the opposite-axis <a lt="min size property">min and max size properties</a> if they are <a>definite</a>),
converted through the aspect ratio
and finally clamped by the same-axis <a>max size property</a> if it's definite.
It is otherwise undefined.
<dt><dfn>content size suggestion</dfn>
The <a>content size suggestion</a> is
the <a>min-content size</a> in the relevant axis,
clamped, if it has an aspect ratio,
by any <a>definite</a> opposite-axis <a lt="min size property">min and max size properties</a>
converted through the aspect ratio,
and then further clamped by the same-axis <a>max size property</a>
if that is <a>definite</a>.
For the purpose of calculating an intrinsic size of the box
(e.g. the box’s <a>min-content size</a>),
a <a>content-based minimum size</a> causes the box’s size in that axis to become indefinite
(even if e.g. its 'width' property specifies a <a>definite</a> size).
Note this means that percentages calculated against this size
will [=behave as auto=].
Nonetheless, although this may require an additional layout pass to re-resolve percentages in some cases,
this value
(like the ''min-content'', ''max-content'', and ''fit-content'' values defined in [[CSS-SIZING-3]])
does not prevent the resolution of percentage sizes within the item.
<div class="note" id="min-size-opt">
Note that while a content-based minimum size is often appropriate,
and helps prevent content from overlapping or spilling outside its container,
in some cases it is not:
In particular, if grid layout is being used for a major content area of a document,
it is better to set an explicit font-relative minimum width such as ''min-width: 12em''.
A content-based minimum width could result in a large table or large image
stretching the size of the entire content area, potentially into an overflow zone,
and thereby making lines of text needlessly long and hard to read.
Note also, when content-based sizing is used on an item with large amounts of content,
the layout engine must traverse all of this content before finding its minimum size,
whereas if the author sets an explicit minimum, this is not necessary.
(For items with small amounts of content, however,
this traversal is trivial and therefore not a performance concern.)
<h2 id='grid-definition'>
Defining the Grid</h2>
████████ ██ ██ ████████ ██ ████ ██████ ████ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ███ ████████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████████ ██ ██ ██ ████████ ████ ██████ ████ ██
<h3 id='explicit-grids'>
The Explicit Grid</h3>
The three properties 'grid-template-rows', 'grid-template-columns', and 'grid-template-areas'
together define the <dfn export local-lt="explicit">explicit grid</dfn> of a <a>grid container</a>
by specifying its <dfn lt="explicit grid track | explicit grid row | explicit grid column">explicit grid tracks</dfn>.
The final grid may end up larger due to <a>grid items</a> placed outside the <a>explicit grid</a>;
in this case implicit tracks will be created,
these implicit tracks will be sized by the 'grid-auto-rows' and 'grid-auto-columns' properties.
The size of the <a>explicit grid</a> is determined by the larger of
the number of rows/columns defined by 'grid-template-areas'
and the number of rows/columns sized by 'grid-template-rows'/'grid-template-columns'.
Any rows/columns defined by 'grid-template-areas' but not sized by 'grid-template-rows'/'grid-template-columns'
take their size from the 'grid-auto-rows'/'grid-auto-columns' properties.
If these properties don't define <em>any</em> <a>explicit</a> tracks
the <a>explicit grid</a> still contains one <a>grid line</a> in each axis.
Numeric indexes in the <a>grid-placement properties</a>
count from the edges of the <a>explicit grid</a>.
Positive indexes count from the <a>start</a> side
(starting from 1 for the <a>start</a>-most <a>explicit</a> line),
while negative indexes count from the <a>end</a> side
(starting from -1 for the <a>end</a>-most <a>explicit</a> line).
The 'grid' and 'grid-template' properties are <a>shorthands</a>
that can be used to set all three <dfn>explicit grid properties</dfn>
('grid-template-rows', 'grid-template-columns', and 'grid-template-areas')
at the same time.
The 'grid' shorthand also resets properties controlling the <a>implicit grid</a>,
whereas the 'grid-template' property leaves them unchanged.
<h3 id='track-sizing'>
Explicit Track Sizing: the 'grid-template-rows' and 'grid-template-columns' properties</h3>
<pre class='propdef'>
Name: grid-template-columns, grid-template-rows
Value: none | <<track-list>> | <<auto-track-list>>
Initial: none
Applies to: <a>grid containers</a>
Inherited: no
Percentages: refer to corresponding dimension of the content area
Computed value: the keyword ''grid-template-columns/none'' or a [=computed track list=]
Animation type: if the list lengths match, by computed value type per item in the [=computed track list=] (see [[#computed-tracks]] and [[#repeat-interpolation]]); discrete otherwise
These properties specify,
as a space-separated <dfn export>track list</dfn>,
the line names and <a>track sizing functions</a> of the <a>grid</a>.
The 'grid-template-columns' property specifies the <a>track list</a> for the grid's columns,
while 'grid-template-rows' specifies the <a>track list</a> for the grid's rows.
Values have the following meanings:
<dl dfn-type="value" dfn-for="grid-template-rows, grid-template-columns">
Indicates that no <a>explicit</a> grid tracks are created by this property
(though <a>explicit grid</a> tracks could still be created by 'grid-template-areas').
Note: In the absence of an <a>explicit grid</a> any rows/columns will be [[#implicit-grids|implicitly generated]],
and their size will be determined by the 'grid-auto-rows' and 'grid-auto-columns' properties.
<dt><dfn id="track-listing"><<track-list>> | <<auto-track-list>></dfn>
Specifies the <a>track list</a> as a series of <a>track sizing functions</a>
and line names.
Each <dfn dfn lt="track sizing function|sizing function">track sizing function</dfn> can be specified as a length,
a percentage of the <a>grid container</a>’s size,
a measurement of the contents occupying the column or row,
or a fraction of the free space in the grid.
It can also be specified as a range using the ''minmax()'' notation,
which can combine any of the previously mentioned mechanisms
to specify separate <a lt="min track sizing function">min</a>
and <a>max track sizing functions</a> for the column or row.
The syntax of a <a>track list</a> is:
<pre class=prod>
<dfn>&lt;track-list></dfn> = [ <<line-names>>? [ <<track-size>> | <<track-repeat>> ] ]+ <<line-names>>?
<dfn>&lt;auto-track-list></dfn> = [ <<line-names>>? [ <<fixed-size>> | <<fixed-repeat>> ] ]* <<line-names>>? <<auto-repeat>>
[ <<line-names>>? [ <<fixed-size>> | <<fixed-repeat>> ] ]* <<line-names>>?
<dfn>&lt;explicit-track-list></dfn> = [ <<line-names>>? <<track-size>> ]+ <<line-names>>?
<dfn>&lt;track-size></dfn> = <<track-breadth>> | minmax( <<inflexible-breadth>> , <<track-breadth>> ) | fit-content( <<length-percentage>> )
<dfn>&lt;fixed-size></dfn> = <<fixed-breadth>> | minmax( <<fixed-breadth>> , <<track-breadth>> ) | minmax( <<inflexible-breadth>> , <<fixed-breadth>> )
<dfn>&lt;track-breadth></dfn> = <<length-percentage>> | <<flex>> | min-content | max-content | auto
<dfn>&lt;inflexible-breadth></dfn> = <<length-percentage>> | min-content | max-content | auto
<dfn>&lt;fixed-breadth></dfn> = <<length-percentage>>
<dfn>&lt;line-names></dfn> = '[' <<custom-ident>>* ']'
Where the component values are defined as follows&hellip;
<h4 id="track-sizes">
Track Sizes</h4>
<dl dfn-for="grid-template-columns, grid-template-rows" dfn-type=value>
A non-negative length or percentage, as defined by CSS3 Values. [[!CSS-VALUES-3]]
<<percentage>> values are relative to
the <a lt="inner size">inner</a> <a>inline size</a>
of the <a>grid container</a> in column <a>grid tracks</a>,
and the <a lt="inner size">inner</a> <a>block size</a>
of the <a>grid container</a> in row <a>grid tracks</a>.
If the size of the <a>grid container</a>
depends on the size of its tracks,
then the <<percentage>>
must be treated as <a value for=width>auto</a>,
for the purpose of calculating the intrinsic sizes of the <a>grid container</a>
and then resolve against that resulting <a>grid container</a> size
for the purpose of laying out the <a>grid</a> and its items.
A non-negative dimension with the unit ''fr'' specifying the track's <dfn dfn noexport>flex factor</dfn>.
Each <<flex>>-sized track takes a share of the remaining space in proportion to its <a>flex factor</a>.
For example, given a track listing of ''1fr 2fr'',
the tracks will take up ⅓ and ⅔ of the <a>leftover space</a>, respectively.
See [[#fr-unit]] for more details.
Note: If the sum of the <a>flex factors</a> is less than 1,
they'll take up only a corresponding fraction of the <a>leftover space</a>,
rather than expanding to fill the entire thing.
When appearing outside a ''minmax()'' notation,
implies an automatic minimum (i.e. ''minmax(auto, <<flex>>)'').
<dt><dfn lt="minmax()">minmax(<var>min</var>, <var>max</var>)</dfn>
Defines a size range
greater than or equal to <var>min</var>
and less than or equal to <var>max</var>.
If the <var>max</var> is less than the <var>min</var>,
then the <var>max</var> will be floored by the <var>min</var>
(essentially yielding ''minmax(<var>min</var>, <var>min</var>)'').
As a maximum, a <<flex>> value sets the track's <a>flex factor</a>;
it is invalid as a minimum.
Note: A future level of this spec may allow <<flex>> minimums,
and will update the <a>track sizing algorithm</a> to account for this correctly
As a <em>maximum</em>: represents the largest <a>max-content contribution</a>
of the <a>grid items</a> occupying the <a>grid track</a>;
however, unlike ''max-content'',
allows expansion of the track
by the 'align-content' and 'justify-content' properties.
As a <em>minimum</em>: represents the largest <a>minimum size</a>
(specified by 'min-width'/'min-height')
of the <a>grid items</a> occupying the <a>grid track</a>.
(This initially is often, but not always,
equal to a ''min-content'' minimum--
see [[#min-size-auto]].)
When appearing outside a ''minmax()'' notation:
equivalent to ''minmax(auto, auto)'',
representing the range between
the minimum and maximum described above.
(This behaves similar to ''minmax(min-content, max-content)'' in the most basic cases,
but with extra abilities.)
Represents the largest <a>max-content contribution</a>
of the <a>grid items</a> occupying the <a>grid track</a>.
Represents the largest <a>min-content contribution</a>
of the <a>grid items</a> occupying the <a>grid track</a>.
<dt><dfn lt="fit-content()">fit-content( <<length-percentage>> )</dfn>
Represents the formula
<code>max(<var>minimum</var>, min(<var>limit</var>, ''max-content''))</code>,
where <var>minimum</var> represents an ''grid-template-columns/auto'' minimum
(which is often, but not always, equal to a ''min-content'' minimum),
and <var>limit</var> is the <a>track sizing function</a>
passed as an argument to ''fit-content()''.
This is essentially calculated as the smaller of
''minmax(auto, max-content)'' and ''minmax(auto, <var>limit</var>)''.
<div class='example'>
Given the following 'grid-template-columns' declaration:
<pre>grid-template-columns: 100px 1fr max-content minmax(min-content, 1fr);</pre>
Five grid lines are created:
At the start edge of the <a>grid container</a>.
100px from the start edge of the <a>grid container</a>.
A distance from the previous line equal to half the <a>free space</a>
(the width of the <a>grid container</a>, minus the width of the non-flexible <a>grid tracks</a>).
A distance from the previous line equal to the maximum size of any <a>grid items</a>
belonging to the column between these two lines.
A distance from the previous line at least as large as the largest minimum size of any <a>grid items</a>
belonging to the column between these two lines,
but no larger than the other half of the <a>free space</a>.
If the non-flexible sizes
(''100px'', ''max-content'', and ''min-content'')
sum to larger than the <a>grid container</a>’s width,
the final <a>grid line</a> will be a distance equal to their sum away from the start edge of the <a>grid container</a>
(the ''1fr'' sizes both resolve to ''0'').
If the sum is less than the <a>grid container</a>’s width,
the final <a>grid line</a> will be exactly at the end edge of the <a>grid container</a>.
This is true in general whenever there's at least one <<flex>> value among the <a>grid track</a> sizes.
<div class='example'>
Additional examples of valid <a>grid track</a> definitions:
/* examples of valid track definitions */
grid-template-rows: 1fr minmax(min-content, 1fr);
grid-template-rows: 10px repeat(2, 1fr auto minmax(30%, 1fr));
grid-template-rows: calc(4em - 5px);
Note: The size of the grid is not purely the sum of the track sizes,
as 'row-gap', 'column-gap' and 'justify-content', 'align-content'
can add additional space between tracks.
██ ██ ███ ██ ██ ████████ ████████ ██ ████ ██ ██ ████████ ██████
███ ██ ██ ██ ███ ███ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██
████ ██ ██ ██ ████ ████ ██ ██ ██ ██ ██ ████ ██ ██ ██
██ ██ ██ ██ ██ ██ ███ ██ ██████ ██ ██ ██ ██ ██ ██ ██ ██████ ██████
██ ████ █████████ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██
██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
██ ██ ██ ██ ██ ██ ████████ ████████ ████████ ████ ██ ██ ████████ ██████
<h4 id='named-lines'>
Naming Grid Lines: the <css>[<<custom-ident>>*]</css> syntax</h4>
While <a>grid lines</a> can always be referred to by their numerical index,
<dfn export lt="line name">line names</dfn>
can make the <a>grid-placement properties</a> easier to understand and maintain.
[=Line names=] can be <dfn lt="explicitly-assigned line name">explicitly assigned</dfn> with the 'grid-template-rows' and 'grid-template-columns' properties,
or [=implicitly-assigned line name|implicitly assigned=] by <a>named grid areas</a> with the 'grid-template-areas' property.
<div class='example'>
For example,
the following code gives meaningful names to all of the lines in the grid.
Note that some of the lines have multiple names.
#grid {
display: grid;
grid-template-columns: [first nav-start] 150px [main-start] 1fr [last];
grid-template-rows: [first header-start] 50px [main-start] 1fr [footer-start] 50px [last];
<figure class="figure">
<img class="figure" alt="Image: Named Grid Lines." src="images/grid-named-lines.png" />
<figcaption>Named Grid Lines.</figcaption>
A line name cannot be <css>span</css> or <css>auto</css>,
i.e. the <<custom-ident>> in the <<line-names>> production
excludes the keywords <css>span</css> and <css>auto</css>.
████████ ████████ ████████ ████████ ███ ████████ ███ ███
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████████ ██████ ████████ ██████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ █████████ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ████████ ██ ████████ ██ ██ ██ ███ ███
<h4 id='repeat-notation'>
Repeating Rows and Columns: the ''repeat()'' notation</h4>
The <dfn>repeat()</dfn> notation represents a repeated fragment of the <a>track list</a>,
allowing a large number of columns or rows that exhibit a recurring pattern
to be written in a more compact form.
<div class='example'>
This example shows two equivalent ways of writing the same grid definition.
Both declarations produce four “main” columns, each 250px wide,
surrounded by 10px “gutter” columns.
grid-template-columns: 10px [col-start] 250px [col-end]
10px [col-start] 250px [col-end]
10px [col-start] 250px [col-end]
10px [col-start] 250px [col-end] 10px;
/* same as above, except easier to write */
grid-template-columns: repeat(4, 10px [col-start] 250px [col-end]) 10px;
<h5 id='repeat-syntax'>
Syntax of ''repeat()''</h5>
The generic form of the ''repeat()'' syntax is, approximately,
<pre class='prod'>
repeat( [ <<integer [1,Infinity]>> | auto-fill | auto-fit ] , <<track-list>> )
The first argument specifies the number of repetitions.
The second argument is a <a>track list</a>,
which is repeated that number of times.
However, there are some restrictions:
* The ''repeat()'' notation can’t be nested.
* Automatic repetitions (''auto-fill'' or ''auto-fit'')
cannot be combined with
<a lt="intrinsic sizing function">intrinsic</a>
or <a lt="flexible sizing function">flexible</a> sizes.
Thus the precise syntax of the ''repeat()'' notation
has several forms:
<pre class='prod'>
<dfn><<track-repeat>></dfn> = repeat( [ <<integer [1, Infinity]>> ] , [ <<line-names>>? <<track-size>> ]+ <<line-names>>? )
<dfn><<auto-repeat>></dfn> = repeat( [ auto-fill | auto-fit ] , [ <<line-names>>? <<fixed-size>> ]+ <<line-names>>? )
<dfn><<fixed-repeat>></dfn> = repeat( [ <<integer [1, Infinity]>> ] , [ <<line-names>>? <<fixed-size>> ]+ <<line-names>>? )
* The <<track-repeat>> variant can represent the repetition of any <<track-size>>,
but is limited to a fixed number of repetitions.
* The <<auto-repeat>> variant can repeat automatically to fill a space,
but requires <a>definite</a> track sizes so that the number of repetitions can be calculated.
It can only appear once in the <a>track list</a>,
but the same <a>track list</a> can also contain <<fixed-repeat>>s.
If a ''repeat()'' function
ends up placing two <<line-names>> adjacent to each other,
the name lists are merged.
For example, ''repeat(2, [a] 1fr [b])'' is equivalent to ''[a] 1fr [b a] 1fr [b]''.
<h5 id="auto-repeat">
Repeat-to-fill: ''auto-fill'' and ''auto-fit'' repetitions</h5>
When <dfn value for="repeat()">auto-fill</dfn> is given as the repetition number,
if the <a>grid container</a> has a <a>definite</a> size or max size in the relevant axis,
then the number of repetitions is the largest possible positive integer
that does not cause the <a>grid</a> to overflow
the <a>content box</a> of its <a>grid container</a>
(treating each track as its <a>max track sizing function</a> if that is <a>definite</a>
or as its minimum track sizing function otherwise,
flooring the <a>max track sizing function</a> by the <a>min track sizing function</a> if both are <a>definite</a>,
and taking 'gap' into account);
if any number of repetitions would overflow,
then 1 repetition.
Otherwise, if the <a>grid container</a> has a <a>definite</a> min size in the relevant axis,
the number of repetitions is the smallest possible positive integer that fulfills that minimum requirement.
Otherwise, the specified <a>track list</a> repeats only once.
<div class="example">
For example, the following code will create
as many 25-character columns as will fit into the window width.
If there is any remaining space,
it will be distributed among the 25-character columns.
body {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(25ch, 1fr));
The <dfn value for="repeat()">auto-fit</dfn> keyword behaves the same as ''auto-fill'',
except that after [[#auto-placement-algo|grid item placement]]
any empty repeated tracks are <a>collapsed</a>.
An empty track is one with no in-flow grid items placed into or spanning across it.
(This can result in <em>all</em> tracks being <a>collapsed</a>,
if they're all empty.)
A <dfn local-lt=collapse>collapsed track</dfn> is treated as having
a fixed <a>track sizing function</a> of ''0px'',
and the <a>gutters</a> on either side of it--
including any space allotted through <a>distributed alignment</a>--
<a lt="collapsed gutter">collapse</a>.
For the purpose of finding the number of auto-repeated tracks,
the UA must floor the track size to a UA-specified value
to avoid division by zero.
It is suggested that this floor be ''1px''.
<h5 id="repeat-interpolation">
Interpolation/Combination of ''repeat()''</h5>
If two ''repeat()'' notations that
have the same first argument (repetition count)
and the same number of tracks in their second argument (the track listing),
they are combined by
combining each component of their <a>computed track lists</a>
<a>by computed value</a>
(just like combining a top-level track list).
They otherwise combine <a lt=discrete>discretely</a>.
████████ ████████ ██ ██ ██ ██ ████ ████████
██ ██ ██ ██ ██ ███ ██ ██ ██
██ ██ ██ ██ ██ ████ ██ ██ ██
██████ ████████ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ███ ██ ██
██ ██ ██ ███████ ██ ██ ████ ██
<h4 id='fr-unit'>
Flexible Lengths: the ''fr'' unit</h4>
A <dfn export>flexible length</dfn> or <dfn><<flex>></dfn> is a dimension
with the <dfn value for="<flex>" lt="fr|fr unit">fr</dfn> unit,
which represents a fraction of the <a>leftover space</a> in the <a>grid container</a>.
Tracks sized with ''fr'' units are called <dfn>flexible tracks</dfn>
as they flex in response to <a>leftover space</a>
similar to how <a>flex items</a> with a zero base size fill space in a <a>flex container</a>.
The distribution of <a>leftover space</a> occurs after all non-flexible <a>track sizing functions</a> have reached their maximum.
The total size of such rows or columns is subtracted from the available space, yielding the <a>leftover space</a>,
which is then divided among the flex-sized rows and columns in proportion to their <a>flex factor</a>.
Each column or row's share of the <a>leftover space</a> can be computed as the column or row's
<code>&lt;flex> * &lt;leftover space> / &lt;sum of all <a>flex factors</a>&gt;</code>.
<details class=note>
<summary><<flex>> values between 0fr and 1fr have a somewhat special behavior:
when the sum of the flex factors is less than 1,
they will take up less than 100% of the leftover space.</summary>
A track’s <<flex>> value
is effectively a request for some proportion of the leftover space,
with ''1fr'' meaning “100% of the leftover space”;
then if the tracks in that axis are requesting more than 100% in total,
the requests are rebalanced to keep the same ratio but use up exactly 100% of it.
However, if the tracks request <em>less</em> than the full amount
(such as three tracks that are each ''.25fr'')
then they'll each get exactly what they request
(25% of the leftover space to each,
with the final 25% left unfilled).
See [[#algo-flex-tracks]] for the exact details
of how leftover space is distributed.
This pattern is required for continuous behavior as ''fr'' values approach zero
(which means the tracks wants <em>none</em> of the leftover space).
Without this, a ''1fr'' track would take all of the leftover space;
but so would a ''0.1fr'' track,
and a ''0.01fr'' track,
until finally the value is small enough to underflow to zero
and the track suddenly takes up none of the leftover space.
With this behavior,
the track instead gradually takes less of the leftover space
as its flex factor shrinks below ''1fr'',
smoothly transitioning to taking none of the leftover space at zero.
Unless this “partial fill” behavior is <em>specifically</em> what's desired,
authors should stick to values &ge; 1;
for example, using ''1fr'' and ''2fr'' is usually better
than using ''.33fr'' and ''.67fr'',
as they're more likely to behave as intended
if tracks are added or removed.
When the available space is infinite
(which happens when the <a>grid container</a>’s width or height is <a>indefinite</a>),
flex-sized <a>grid tracks</a> are sized to their contents while retaining their respective proportions.
The used size of each flex-sized <a>grid track</a> is computed by
determining the ''max-content'' size of each flex-sized <a>grid track</a>
and dividing that size by the respective <a>flex factor</a>
to determine a “hypothetical ''1fr'' size”.
The maximum of those is used as the resolved ''1fr'' length (the <dfn>flex fraction</dfn>),
which is then multiplied by each <a>grid track</a>’s <a>flex factor</a> to determine its final size.
Note: <<flex>> values are not <<length>>s
(nor are they compatible with <<length>>s, like some <<percentage>> values),
so they cannot be represented in or combined with other unit types in ''calc()'' expressions.
██████ ███████ ██ ██ ████████ ██ ██ ████████ ████████ ████████
██ ██ ██ ██ ███ ███ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ████ ████ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ███ ██ ████████ ██ ██ ██ ██████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ███████ ██ ██ ██ ███████ ██ ████████ ████████
<h4 id='computed-tracks'>
Computed Value of a Track Listing</h4>
A <dfn>computed track list</dfn>
is a <a spec=infra>list</a>
alternating between [=line name sets=] and [=track sections=],
with the first and last items being [=line name sets=].
A <dfn>line name set</dfn> is a (potentially empty) <a spec=infra for=/>set</a>
of identifiers representing line names.
A <dfn>track section</dfn> is either:
* a ''minmax()'' functional notation representing a single track's size,
with each <<length-percentage>> computed
* a ''repeat()'' functional notation representing a repeated track list section,
with its <<integer>> computed
and its <<track-list>> represented as a [=computed track list=]
████████ ████████ ██████ ███████ ██ ██ ██ ████████ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████████ ██████ ██████ ██ ██ ██ ██ ██ ██████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ████████ ██████ ███████ ████████ ███ ████████ ████████
<h4 id='resolved-track-list'>
Resolved Value of a Track Listing</h4>
The 'grid-template-rows' and 'grid-template-columns' properties are
<a>resolved value special case properties</a>. [[!CSSOM]]
When an element generates a [=grid container=] box,
the <a href="">resolved value</a>
of its 'grid-template-rows' or 'grid-template-columns' property
is the <a>used value</a>,
serialized with:
Every track listed individually,
whether implicitly or explicitly created,
without using the ''repeat()'' notation.
Every track size given as a length in pixels,
regardless of sizing function.
Adjacent line names collapsed into a single bracketed set.
<div class=issue>
The first bullet point of the above list
means that implicit tracks get serialized
as part of 'grid-template-rows'/etc.,
despite the fact that an author <em>cannot</em>
actually specify implicit track sizes in those properties!
So 'grid-template-rows' and 'grid-template-columns' values
might not round-trip correctly:
<pre highlight=js>
const s = getComputedStyle(gridEl); = s.gridTemplateRows;
// Code like this <em>should</em> be a no-op,
// but if there are any implicit rows,
// this will convert them into explicit rows,
// possibly changing how grid items are positioned
// and altering the overall size of the grid!
This is an accidental property of an early implementation
that leaked into later implementations
without much thought given to it.
We intend to remove it from the spec,
but not until after we've defined a CSSOM API
for getting information about implicit tracks,
as currently this is the only way to get that information
and a number of pages rely on that.
Otherwise, (e.g. when the element has ''display: none'' or is not a <a>grid container</a>)
the resolved value is simply the <a>computed value</a>.
<div class="example">
<pre class="lang-html">
#grid {
width: 500px;
[a] auto
[b] minmax(min-content, 1fr)
[b c d] repeat(2, [e] 40px)
repeat(5, auto);
&lt;div id="grid">
&lt;div style="grid-column-start: 1; width: 50px">&lt;/div>
&lt;div style="grid-column-start: 9; width: 50px">&lt;/div>
var gridElement = document.getElementById("grid");
// [a] 50px [b] 320px [b c d e] 40px [e] 40px 0px 0px 0px 0px 50px
Note: In general, resolved values are the computed values,
except for a small list of legacy 2.1 properties.
However, compatibility with early implementations of this module
requires us to define 'grid-template-rows' and 'grid-template-columns' as returning used values.
ISSUE: The CSS Working Group is considering whether to also return used values
for the <a>grid-placement properties</a>
and is looking for feedback, especially from implementors.
See <a href="">discussion</a>.
██████ ████████ ████ ████████ ███ ████████ ████████ ███ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ████ ████████ ██ ██ ██ ██ ██ ████████ ██████ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ █████████ ██ ██ ██ █████████ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ████ ████████ ██ ██ ██ ██ ████████ ██ ██ ██████
<h3 id="grid-template-areas-property">
Named Areas: the 'grid-template-areas' property</h3>
<pre class='propdef'>
Name: grid-template-areas
Value: none | <<string>>+
Initial: none
Applies to: <a>grid containers</a>
Inherited: no
Percentages: n/a
Computed value: the keyword ''grid-template-areas/none'' or a list of string values
Animation type: discrete
This property specifies <dfn export lt="named grid area">named grid areas</dfn>,
which are not associated with any particular <a>grid item</a>,
but can be referenced from the <a>grid-placement properties</a>.
The syntax of the 'grid-template-areas' property also provides a visualization
of the structure of the <a>grid</a>,
making the overall layout of the <a>grid container</a> easier to understand.
Values have the following meanings:
<dl dfn-for="grid-template-areas" dfn-type=value>
Indicates that no <a>named grid areas</a>,
and likewise no <a>explicit grid</a> tracks,
are defined by this property
(though <a>explicit grid</a> tracks could still be created by 'grid-template-columns' or 'grid-template-rows').
Note: In the absence of an <a>explicit grid</a> any rows/columns will be [[#implicit-grids|implicitly generated]],
and their size will be determined by the 'grid-auto-rows' and 'grid-auto-columns' properties.
A row is created for every separate string listed for the 'grid-template-areas' property,
and a column is created for each cell in the string,
when parsed as follows:
Tokenize the string into a list of the following tokens,
using longest-match semantics:
<ul dfn-type="dfn">
A sequence of <a>name code points</a>,
representing a <dfn>named cell token</dfn>
with a name consisting of its code points.
A sequence of one or more "." (U+002E FULL STOP),
representing a <dfn>null cell token</dfn>.
A sequence of <a>whitespace</a>,
representing nothing
(do not produce a token).
A sequence of any other characters,
representing a <dfn>trash token</dfn>.
Note: These rules can produce cell names that do not match the <<ident>> syntax,
such as "1st 2nd 3rd",
which requires escaping when referencing those areas by name in other properties,
like ''grid-row: \31st;'' to reference the area named ''1st''.
A <a>null cell token</a> represents an unnamed area in the <a>grid container</a>.
A <a>named cell token</a> creates a <a>named grid area</a> with the same name.
Multiple <a>named cell tokens</a> within and between rows
create a single <a>named grid area</a> that spans the corresponding <a>grid cells</a>.
A <a>trash token</a> is a syntax error,
and makes the declaration invalid.
All strings must define the same number of cell tokens
([=named cell tokens=] and/or [=null cell tokens=]),
and at least one cell token,
or else the declaration is invalid.
If a <a>named grid area</a> spans multiple <a>grid cells</a>,
but those cells do not form a single filled-in rectangle,
the declaration is invalid.
Note: Non-rectangular or disconnected regions may be permitted in a future version of this module.
<div class='example'>
In this example, the 'grid-template-areas' property is used to create a page layout
where areas are defined for header content (<code>head</code>),
navigational content (<code>nav</code>),
footer content (<code>foot</code>),
and main content (<code>main</code>).
Accordingly, the template creates three rows and two columns,
with four <a>named grid areas</a>.
The <code>head</code> area spans both columns and the first row of the grid.
#grid {
display: grid;
grid-template-areas: "head head"
"nav main"
"foot ...."
#grid > header { grid-area: head; }
#grid > nav { grid-area: nav; }
#grid > main { grid-area: main; }
#grid > footer { grid-area: foot; }
<h4 id="serialize-template">
Serialization Of Template Strings</h4>
When serializing either the [=specified value|specified=] or [=computed value=] of a <<string>> value of 'grid-template-areas',
each [=null cell token=] is serialized as a single "." (U+002E FULL STOP),
and consecutive cell tokens are separated by a single space (U+0020 SPACE),
with all other white space elided.
<h4 id="implicit-named-lines">
Implicitly-Assigned Line Names</h4>
The 'grid-template-areas' property generates <dfn export lt="implicitly-assigned line name">implicitly-assigned line names</dfn> from the <a>named grid areas</a> in the template.
For each <a>named grid area</a> <var>foo</var>, four <a>implicitly-assigned line names</a> are created:
two named <css><var>foo</var>-start</css>, naming the row-start and column-start lines of the <a>named grid area</a>,
and two named <css><var>foo</var>-end</css>, naming the row-end and column-end lines of the <a>named grid area</a>.
These [=implicitly-assigned line names=] behave just like any other [=line names=],
except that they do not appear in the value of 'grid-template-rows'/'grid-template-columns'.
Even if an [=explicitly-assigned line name=] with the same name is defined,
the [=implicitly-assigned line names=] are just more lines with the same name.
<h4 id="implicit-named-areas">
Implicitly-Named Areas</h4>
Since a <a>named grid area</a> is referenced by the <a>implicitly-assigned line names</a> it produces,
explicitly adding named lines of the same form (''foo-start''/''foo-end'')
effectively creates a <a>named grid area</a>.
Such <dfn export lt="implicitly-named area">implicitly-named areas</dfn> do not appear in the value of 'grid-template-areas',
but can still be referenced by the <a>grid-placement properties</a>.
██████ ████████ ████ ████████ ████████ ████████ ██ ██ ████████ ██ ███ ████████ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ███ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██ ██ ██ ██ ██ ██ ██
██ ████ ████████ ██ ██ ██ ███████ ██ ██████ ██ ███ ██ ████████ ██ ██ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █████████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ████ ████████ ██ ████████ ██ ██ ██ ████████ ██ ██ ██ ████████
<h3 id='explicit-grid-shorthand'>
Explicit Grid Shorthand: the 'grid-template' property</h3>
<pre class='propdef shorthand'>
Name: grid-template
Value: none |
Value: <nobr>[ <<'grid-template-rows'>> / <<'grid-template-columns'>> ]</nobr> |
Value: <nobr>[ <<line-names>>? <<string>> <<track-size>>? <<line-names>>? ]+ [ / <<explicit-track-list>> ]?</nobr>
Initial: none
Applies to: <a>grid containers</a>
The 'grid-template' property is a <a>shorthand</a> for setting
'grid-template-columns', 'grid-template-rows', and 'grid-template-areas' in a single declaration.
It has several distinct syntax forms:
<dl dfn-for=grid-template dfn-type=value>
Sets all three properties to their initial values (''grid-template-rows/none'').
<dt><dfn id='grid-template-rowcol'><<'grid-template-rows'>> / <<'grid-template-columns'>></dfn>
Sets 'grid-template-rows' and 'grid-template-columns' to the specified values, respectively,
and sets 'grid-template-areas' to ''grid-template-areas/none''.
<div class='example'>
<pre>grid-template: auto 1fr / auto 1fr auto;</pre>
is equivalent to
grid-template-rows: auto 1fr;
grid-template-columns: auto 1fr auto;
grid-template-areas: none;
<dt><dfn id='grid-template-ascii'>[ <<line-names>>? <<string>> <<track-size>>? <<line-names>>? ]+ [ / <<explicit-track-list>> ]?</dfn>
* Sets 'grid-template-areas' to the strings listed.
* Sets 'grid-template-rows' to the <<track-size>>s following each string
(filling in ''grid-template-rows/auto'' for any missing sizes),
and splicing in the named lines defined before/after each size.
* Sets 'grid-template-columns' to the track listing specified after the slash
(or ''grid-template-columns/none'', if not specified).
This syntax allows the author to align track names and sizes inline with their respective grid areas.
<div class='example'>
grid-template: [header-top] "a a a" [header-bottom]
[main-top] "b b b" 1fr [main-bottom]
/ auto 1fr auto;
is equivalent to
grid-template-areas: "a a a"
"b b b";
grid-template-rows: [header-top] auto [header-bottom main-top] 1fr [main-bottom];
grid-template-columns: auto 1fr auto;
and creates the following grid:
<object data="images/grid-shorthand.svg" type="image/svg+xml">
<li>Three columns, sized ''auto'', ''1fr'', and ''auto'', respectively
<li>Two rows sized as ''auto'' and ''1fr'', respectively.
<li>A line named both “header-top” and “a-start” at the top,
a line with four names--
“header-bottom”, “main-top”, “a-end”, and “b-start”--
in the middle,
a line named “main-bottom” and “b-end” at the bottom.
<li>A line named “a-start” and “b-start” on the left edge,
and a line named “a-end” and “b-end” on the right edge.
<figcaption>The grid created by the declarations above.
(The “a/b-start/end” names are [=implicitly-assigned line name|implicitly assigned=] by the <a>named grid areas</a>.)
Note: Note that the ''repeat()'' function isn't allowed in these track listings,
as the tracks are intended to visually line up one-to-one with the rows/columns in the “ASCII art”.
Note: The 'grid' shorthand accepts the same syntax,
but also resets the implicit grid properties to their initial values.
Unless authors want those to cascade in separately,
it is therefore recommended to use 'grid' instead of 'grid-template'.
████ ██ ██ ████████ ██ ████ ██████ ████ ████████
██ ███ ███ ██ ██ ██ ██ ██ ██ ██ ██
██ ████ ████ ██ ██ ██ ██ ██ ██ ██
██ ██ ███ ██ ████████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████ ██ ██ ██ ████████ ████ ██████ ████ ██
<h3 id='implicit-grids'>
The Implicit Grid</h3>
The 'grid-template-rows', 'grid-template-columns', and 'grid-template-areas' properties define a fixed number
of tracks that form the <a>explicit grid</a>.
When <a>grid items</a> are positioned outside of these bounds,
the <a>grid container</a> generates
<dfn export lt="implicit grid track|implicit grid row|implicit grid column">implicit grid tracks</dfn>
by adding <dfn>implicit grid lines</dfn> to the <a>grid</a>.
These lines together with the <a>explicit grid</a> form the <dfn export local-lt="implicit">implicit grid</dfn>.
The 'grid-auto-rows' and 'grid-auto-columns' properties size these [=implicit grid tracks=],
as well as any [=explicit grid tracks=] created by 'grid-template-areas'
but not explicitly sized by 'grid-template-rows' or 'grid-template-columns'
The 'grid-auto-flow' property controls auto-placement of <a>grid items</a>
without an explicit position.
Once the <a>explicit grid</a> is filled
(or if there is no <a>explicit grid</a>)
auto-placement will also cause the generation of <a>implicit grid tracks</a>.
The 'grid' <a>shorthand</a> property can set the <dfn>implicit grid properties</dfn>
('grid-auto-flow', 'grid-auto-rows', and 'grid-auto-columns')
together with the <a>explicit grid properties</a>
in a single declaration.
███ ██ ██ ████████ ███████ ████████ ████████ ███ ██████ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ████████ ██ ██ ██ █████ ██████
█████████ ██ ██ ██ ██ ██ ██ ██ ██ █████████ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ███████ ██ ███████ ██ ██ ██ ██ ██ ██████ ██ ██ ██████
<h3 id='auto-tracks'>
Implicit Track Sizing: the 'grid-auto-rows' and 'grid-auto-columns' properties</h3>
<pre class='propdef'>
Name: grid-auto-columns, grid-auto-rows
Value: <<track-size>>+
Initial: auto
Applies to: <a>grid containers</a>
Inherited: no
Percentages: see [[#track-sizing|Track Sizing]]
Computed value: see [[#track-sizing|Track Sizing]]
Animation type: by computed value type
The 'grid-auto-columns' and 'grid-auto-rows' properties specify
the size of tracks not assigned a size
by 'grid-template-rows' or 'grid-template-columns'.
If multiple track sizes are given, the pattern is repeated as necessary
to find the size of the affected tracks.
The first track after the last explicitly-sized track
receives the first specified size, and so on forwards;
and the last <a>implicit grid track</a> before the <a>explicit grid</a>
receives the last specified size, and so on backwards.
Note: If a grid item is positioned into a row or column that is not explicitly declared
by 'grid-template-rows'/'grid-template-columns' and/or 'grid-template-areas',
[=implicit grid tracks=] are created to hold it.
This can happen either by explicitly positioning into a row or column that is out of range,
or by the [=auto-placement algorithm=] creating additional rows or columns.
<div class="example">
<pre class="lang-html">
#grid {
display: grid;
grid-template-columns: 20px;
grid-auto-columns: 40px;
grid-template-rows: 20px;
grid-auto-rows: 40px;
#A { grid-column: 1; grid-row: 1; }
#B { grid-column: 2; grid-row: 1; }
#C { grid-column: 1; grid-row: 2; }
#D { grid-column: 2; grid-row: 2; }
&lt;div id="grid">
&lt;div id="A">A&lt;/div>
&lt;div id="B">B&lt;/div>
&lt;div id="C">C&lt;/div>
&lt;div id="D">D&lt;/div>
<img src="images/auto-flow.svg" alt="">
<figcaption>A 2&times;2 grid with one explicit 20px&times;20px grid cell
in the first row+column
and three additional cells resulting from the implicit 40px column and row
generated to hold the additional grid items.
██████ ████████ ████ ████████ ███ ██ ██ ████████ ███████ ████████ ██ ███████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ████ ████████ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ ███████ ██████ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ █████████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ████ ████████ ██ ██ ███████ ██ ███████ ██ ████████ ███████ ███ ███
<h3 id="grid-auto-flow-property">
<dfn lt="auto-placement|automatic placement">Automatic Placement</dfn>: the 'grid-auto-flow' property</h3>
<pre class='propdef'>
Name: grid-auto-flow
Value: [ row | column ] || dense
Initial: row
Applies to: <a>grid containers</a>
Inherited: no
Percentages: n/a
Computed value: specified keyword(s)
Animation type: discrete
<a>Grid items</a> that aren't explicitly placed are automatically placed
into an unoccupied space in the <a>grid container</a>
by the <a>auto-placement algorithm</a>.
'grid-auto-flow' controls how the <a>auto-placement algorithm</a> works,
specifying exactly how auto-placed items get flowed into the grid.
See [[#auto-placement-algo]] for details on precisely how the auto-placement algorithm works.
<dl dfn-for="grid-auto-flow" dfn-type=value>
The <a>auto-placement algorithm</a> places items
by filling each row in turn,
adding new rows as necessary.
If neither ''row'' nor ''column'' is provided,
''row'' is assumed.
The <a>auto-placement algorithm</a> places items
by filling each column in turn,
adding new columns as necessary.
If specified, the <a>auto-placement algorithm</a> uses a “dense” packing algorithm,
which attempts to fill in holes earlier in the grid if smaller items come up later.
This may cause items to appear out-of-order,
when doing so would fill in holes left by larger items.
If omitted, a “sparse” algorithm is used,
where the placement algorithm only ever moves “forward” in the grid when placing items,
never backtracking to fill holes.
This ensures that all of the auto-placed items appear “in order”,
even if this leaves holes that could have been filled by later items.
Note: A future level of this module is expected to add a value that flows auto-positioned items together into a single “default” cell.
Auto-placement takes <a>grid items</a> in <a>order-modified document order</a>.
<div class='example'>
In the following example, there are three columns, each auto-sized to their contents.
No rows are explicitly defined.
The 'grid-auto-flow' property is ''grid-auto-flow/row''
which instructs the grid to search across its three columns starting with the first row,
then the next,
adding rows as needed until sufficient space is located to accommodate the position of any auto-placed <a>grid item</a>.
<div class="figure">
<img alt="Image: A form arranged using automatic placement." src="images/auto-placed-form.png" />
<p class="caption">A form arranged using automatic placement.
&lt;style type="text/css">
form {
display: grid;
/* Define three columns, all content-sized,
and name the corresponding lines. */
grid-template-columns: [labels] auto [controls] auto [oversized] auto;
grid-auto-flow: row dense;
form > label {
/* Place all labels in the "labels" column and
automatically find the next available row. */
grid-column: labels;
grid-row: auto;
form > input, form > select {
/* Place all controls in the "controls" column and
automatically find the next available row. */
grid-column: controls;
grid-row: auto;
#department-block {
/* Auto place this item in the "oversized" column
in the first row where an area that spans three rows
won't overlap other explicitly placed items or areas
or any items automatically placed prior to this area. */
grid-column: oversized;
grid-row: span 3;
/* Place all the buttons of the form
in the explicitly defined grid area. */
#buttons {
grid-row: auto;
/* Ensure the button area spans the entire grid element
in the inline axis. */
grid-column: 1 / -1;
text-align: end;
&lt;label for="firstname">First name:&lt;/label>
&lt;input type="text" id="firstname" name="firstname" />
&lt;label for="lastname">Last name:&lt;/label>
&lt;input type="text" id="lastname" name="lastname" />
&lt;label for="address">Address:&lt;/label>
&lt;input type="text" id="address" name="address" />
&lt;label for="address2">Address 2:&lt;/label>
&lt;input type="text" id="address2" name="address2" />
&lt;label for="city">City:&lt;/label>
&lt;input type="text" id="city" name="city" />
&lt;label for="state">State:&lt;/label>
&lt;select type="text" id="state" name="state">
&lt;option value="WA">Washington&lt;/option>
&lt;label for="zip">Zip:&lt;/label>
&lt;input type="text" id="zip" name="zip" />
&lt;div id="department-block">
&lt;label for="department">Department:&lt;/label>
&lt;select id="department" name="department" multiple>
&lt;option value="finance">Finance&lt;/option>
&lt;option value="humanresources">Human Resources&lt;/option>
&lt;option value="marketing">Marketing&lt;/option>
&lt;div id="buttons">
&lt;button id="cancel">Cancel&lt;/button>
&lt;button id="back">Back&lt;/button>
&lt;button id="next">Next&lt;/button>
██████ ████████ ████ ████████
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██
██ ████ ████████ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ████ ████████
<h3 id='grid-shorthand'>
Grid Definition Shorthand: the 'grid' property</h3>
<pre class='propdef shorthand'>
Name: grid
Value: <<'grid-template'>> |
Value: <nobr><<'grid-template-rows'>> / [ auto-flow && dense? ] <<'grid-auto-columns'>>?</nobr> |
Value: <nobr>[ auto-flow && dense? ] <<'grid-auto-rows'>>? / <<'grid-template-columns'>></nobr>
Initial: none
Applies to: <a>grid containers</a>
The 'grid' property is a <a>shorthand</a> that sets
all of the [=explicit grid properties=]
('grid-template-rows', 'grid-template-columns', and 'grid-template-areas'),
and all the [=implicit grid properties=]
('grid-auto-rows', 'grid-auto-columns', and 'grid-auto-flow'),
in a single declaration.
(It does not reset the <a>gutter</a> properties.)
Its syntax matches 'grid-template',
plus an additional syntax form
for defining auto-flow grids:
<dl dfn-for=grid dfn-type=value>
<dt><dfn id='grid-s-auto-row'><<'grid-template-rows'>> / [ auto-flow && dense? ] <<'grid-auto-columns'>>?</dfn>
<dt><dfn id='grid-s-auto-column'>[ auto-flow && dense? ] <<'grid-auto-rows'>>? / <<'grid-template-columns'>></dfn>
Sets up auto-flow,
by setting the tracks in one axis explicitly
(setting either 'grid-template-rows' or 'grid-template-columns' as specified,
and setting the other to ''grid-template-rows/none''),
and specifying how to auto-repeat the tracks in the other axis
(setting either 'grid-auto-rows' or 'grid-auto-columns' as specified,
and setting the other to ''grid-auto-rows/auto'').
'grid-auto-flow' is also set to either ''row'' or ''column'' accordingly,
with ''dense'' if it's specified.
All other 'grid' <a>sub-properties</a> are reset to their initial values.
Note: Note that you can only specify the explicit <em>or</em> the implicit grid properties in a single 'grid' declaration.
The sub-properties you don't specify are set to their initial value,
as normal for <a>shorthands</a>.
<div class='example'>
In addition to accepting the 'grid-template' shorthand syntax for setting up the <a>explicit grid</a>,
the 'grid' shorthand can also easily set up parameters for an auto-formatted grid.
For example, ''grid: auto-flow 1fr / 100px;'' is equivalent to
grid-template: none / 100px;
grid-auto-flow: row;
grid-auto-rows: 1fr;
grid-auto-columns: auto;
Similarly, ''grid: none / auto-flow 1fr'' is equivalent to
grid-template: none;
grid-auto-flow: column;
grid-auto-rows: auto;
grid-auto-columns: 1fr;
████████ ██ ███ ██████ ████ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ████ ██ ██
████████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████
██ ██ █████████ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██
██ ████████ ██ ██ ██████ ████ ██ ██ ██████
<h2 id="placement">
Placing Grid Items</h2>
Every <a>grid item</a> is associated with a <a>grid area</a>,
a rectangular set of adjacent <a>grid cells</a> that the <a>grid item</a> occupies.
This <a>grid area</a> defines the <a>containing block</a> for the <a>grid item</a>
within which the self-alignment properties ('justify-self' and 'align-self') determine their actual position.
The cells that a <a>grid item</a> occupies also influence the sizing of the grid's rows and columns,
defined in [[#layout-algorithm]].
The location of a <a>grid item’s</a> <a>grid area</a> within the <a>grid</a>
is defined by its <dfn export lt="grid placement" local-lt="placement">placement</dfn>,
which consists of a <a>grid position</a> and a <a>grid span</a>:
<dl export>
<dt><dfn export local-lt="position|row position|column position">grid position</dfn>
The <a>grid item</a>’s location in the <a>grid</a> in each axis.
A <a>grid position</a> can be either
<dfn export lt="definite grid position|definite row position|definite column position|definite position">definite</dfn> (explicitly specified)
or <dfn export lt="automatic grid position|automatic row position|automatic column position|automatic position">automatic</dfn> (determined by <a>auto-placement</a>).
<dt><dfn export local-lt="span|row span|column span">grid span</dfn>
How many <a>grid tracks</a> the <a>grid item</a> occupies in each axis.
In Level 1,
a <a>grid item</a>’s <a>grid span</a> is always <dfn export lt="definite grid span|definite row span|definite column span|definite span">definite</dfn>,
defaulting to 1 in each axis if it can't be otherwise determined for that axis.
The <dfn export lt="grid-placement property">grid-placement properties</dfn>--
the longhands 'grid-row-start', 'grid-row-end', 'grid-column-start', 'grid-column-end', and their shorthands 'grid-row', 'grid-column', and 'grid-area'--
allow the author to specify a <a>grid item</a>’s <a>placement</a>
by providing any (or none) of the following six pieces of information:
<table class=data style="width: auto">
<td>row-start line
<td>column-start line
<td>row-end line
<td>column-end line
<td>row span
<td>column span
A definite value for any two of <var>Start</var>, <var>End</var>, and <var>Span</var> in a given dimension implies a definite value for the third.
The following table summarizes the conditions under which a grid position or span is <i lt>definite</i> or <i lt>automatic</i>:
<table class='data'>
<td>At least one specified line
<td>Explicit, implicit, or defaulted span.
<td>No lines explicitly specified
<h3 id='common-uses'>
Common Patterns for Grid Placement</h3>
<em>This section is informative.</em>
The <a>grid-placement property</a> longhands are organized into three shorthands:
<table class=data id="grid-property-breakdown">
<td colspan=4>'grid-area'
<td colspan=2>'grid-column'
<td colspan=2>'grid-row'
<h4 id='common-uses-named-areas'>
Named Areas</h4>
An item can be placed into a <a>named grid area</a>
(such as those produced by the template in 'grid-template-areas')
by specifying the area’s name in 'grid-area':
<div class='example'>
article {
grid-area: main;
/* Places item into the named area "main". */
An item can also be <em>partially</em> aligned with a <a>named grid area</a>,
with other edges aligned to some other line:
<div class='example'>
.one {
grid-row-start: main;
/* Align the row-start edge to the start edge of the "main" named area. */
<h4 id='common-uses-numeric'>
Numeric Indexes and Spans</h4>
Grid items can be positioned and sized by number,
which is particularly helpful for script-driven layouts:
<div class='example'>
.two {
grid-row: 2; /* Place item in the second row. */
grid-column: 3; /* Place item in the third column. */
/* Equivalent to grid-area: 2 / 3; */
By default, a grid item has a span of 1.
Different spans can be given explicitly:
<div class='example'>
.three {
grid-row: 2 / span 5;
/* Starts in the 2nd row,
spans 5 rows down (ending in the 7th row). */
.four {
grid-row: span 5 / 7;
/* <em>Ends</em> in the 7th row,
spans 5 rows up (starting in the 2nd row). */
Note: Note that grid indexes are <a>writing mode</a> relative.
For example, in a right-to-left language like Arabic,
the first column is the rightmost column.
<h4 id='common-uses-named-lines'>
Named Lines and Spans</h4>
Instead of counting lines by number,
<a>named lines</a> can be referenced by their name:
<div class='example'>
.five {
grid-column: first / middle;
/* Span from line "first" to line "middle". */
Note: Note that if a <a>named grid area</a> and a <a>named line</a> have the same name,
the placement algorithm will prefer to use <a>named grid area</a>’s lines instead.
If there are multiple lines of the same name,
they effectively establish a named set of grid lines,
which can be exclusively indexed by filtering the placement by name:
<div class='example'>
.six {
grid-row: text 5 / text 7;
/* Span between the 5th and 7th lines named "text". */
grid-row: text 5 / span text 2;
/* Same as above - start at the 5th line named "text",
then span across two more "text" lines, to the 7th. */
<h4 id='common-uses-auto-placement'>
Auto Placement</h4>
A <a>grid item</a> can be automatically placed into the next available empty <a>grid cell</a>,
growing the <a>grid</a> if there's no space left.
<div class='example'>
.eight {
grid-area: auto; /* Initial value */
This can be used, for example, to list a number of sale items on a catalog site
in a grid pattern.
Auto-placement can be combined with an explicit span,
if the item should take up more than one cell:
<div class='example'>
.nine {
grid-area: span 2 / span 3;
/* Auto-placed item, covering two rows and three columns. */
Whether the <a>auto-placement algorithm</a> searches across and adds rows,
or searches across and adds columns,
is controlled by the 'grid-auto-flow' property.
Note: By default, the <a>auto-placement algorithm</a> looks linearly through the grid without backtracking;
if it has to skip some empty spaces to place a larger item,
it will not return to fill those spaces.
To change this behavior,
specify the ''dense'' keyword in 'grid-auto-flow'.
<h3 id="placement-a11y">
Grid Item Placement vs. Source Order</h3>
<p>“With great power comes great responsibility.”
The abilities of the <a>grid-placement properties</a>
allow content to be freely arranged and reordered within the <a>grid</a>,
such that the visual presentation can be largely disjoint
from the underlying document source order.
These abilities allow the author great freedom
in tailoring the rendering to different devices
and modes of presentation
e.g. using <a>media queries</a>.
However <strong>they are not a substitute for correct source ordering</strong>.
Correct source order is important for speech,
for sequential navigation (such as keyboard navigation),
and non-CSS UAs such as search engines, tactile browsers, etc.
Grid placement <em>only</em> affects the visual presentation!
This allows authors to optimize the document source for
non-CSS/non-visual interaction modes,
and use grid placement techniques to further manipulate the visual presentation
so as to leave that source order intact.
██████ ████████ ████ ████████ ████████ ███████ ██ ██ ██████ ████████ ███ ████████ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ████ ████████ ██ ██ ██ ███████ ████████ ██ ██ ██ ██ ██ ███████ ██████ ██ ██ ██ ████████ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █████████ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ████ ████████ ██ ██ ███████ ███ ███ ██████ ██ ██ ██ ██ ██ ██
<h3 id='line-placement'>
Line-based Placement: the 'grid-row-start', 'grid-column-start', 'grid-row-end', and 'grid-column-end' properties</h3>
<pre class='propdef'>
Name: grid-row-start, grid-column-start, grid-row-end, grid-column-end
Value: <<grid-line>>
Initial: auto
Applies to: <a>grid items</a> and absolutely-positioned boxes whose containing block is a <a>grid container</a>
Inherited: no
Percentages: n/a
Computed value: specified keyword, identifier, and/or integer
Animation type: discrete
<pre class=prod>
<dfn for="grid-row-start, grid-column-start, grid-row-end, grid-column-end"><<grid-line>></dfn> =
auto |
<<custom-ident>> |
[ <<integer>> && <<custom-ident>>? ] |
[ span &amp;&amp; [ <<integer>> || <<custom-ident>> ] ]
The 'grid-row-start', 'grid-column-start', 'grid-row-end', and 'grid-column-end' properties
determine a <a>grid item</a>’s size and location within the <a>grid</a>
by contributing a line, a span, or nothing (automatic)
to its <a>grid placement</a>,
thereby specifying the <a>inline-start</a>, <a>block-start</a>, <a>inline-end</a>, and <a>block-end</a> edges of its <a>grid area</a>.
Values have the following meanings:
<dl dfn-for="<grid-line>" dfn-type=value>
<dt><dfn id='grid-placement-slot'><<custom-ident>></dfn>
First attempt to match the <a>grid area</a>’s edge to a <a>named grid area</a>:
if there is a <a>named line</a> with the name <css><<custom-ident>>-start</css> (for 'grid-*-start') / <css><<custom-ident>>-end</css> (for 'grid-*-end'),
contributes the first such line to the <a>grid item</a>’s <a>placement</a>.
Note: <a>Named grid areas</a> automatically generate [=implicitly-assigned line names=] of this form,
so specifying ''grid-row-start: foo'' will choose the start edge of that <a>named grid area</a>
(unless another line named ''foo-start'' was explicitly specified before it).
treat this as if the integer ''1'' had been specified along with the <<custom-ident>>.
<dt><dfn id='grid-placement-int'><<integer>> && <<custom-ident>>?</dfn>
Contributes the <var>N</var>th <a>grid line</a> to the <a>grid item</a>’s <a>placement</a>.
If a negative integer is given,
it instead counts in reverse,
starting from the end edge of the <a>explicit grid</a>.
If a name is given as a <<custom-ident>>,
only lines with that name are counted.
If not enough lines with that name exist,
all <a>implicit grid lines</a>
are assumed to have that name for the purpose of finding this position.
An <<integer>> value of zero makes the declaration invalid.
<dt><dfn id='grid-placement-span-int'>span && [ <<integer>> || <<custom-ident>> ]</dfn>
Contributes a <a>grid span</a> to the <a>grid item</a>’s <a>placement</a>
such that the corresponding edge of the <a>grid item</a>’s <a>grid area</a> is <var>N</var> lines from its opposite edge
in the corresponding direction.
For example, ''grid-column-end: span 2'' indicates the second grid line in the endward direction
from the 'grid-column-start' line.
If a name is given as a <<custom-ident>>,
only lines with that name are counted.
If not enough lines with that name exist,
all <a>implicit grid lines</a>
on the side of the <a>explicit grid</a> corresponding to the search direction
are assumed to have that name for the purpose of counting this span.
<div class="example">
For example, given the following declarations:
<pre class="lang-css">
.grid { grid-template-columns: 100px; }
.griditem { grid-column: span foo / 4; }
The <a>grid container</a> has an <a>explicit grid</a> with two grid lines,
numbered 1 and 2.
The <a>grid item's</a> column-end edge is specified to be at line 4,
so two lines are generated in the endward side of the <a>implicit grid</a>.
Its column-start edge must be the first "foo" line it can find startward of that.
There is no "foo" line in the grid, though,
so the only possibility is a line in the <a>implicit grid</a>.
Line 3 is not a candidate, because it's on the endward side of the <a>explicit grid</a>,
while the 'grid-column-start' span forces it to search startward.
So, the only option is for the <a>implicit grid</a> to generate a line on the startward side of the <a>explicit grid</a>.
<img src="images/implicit-lines-search.svg" width=600>
<figcaption>An illustration of the result.
If the <<integer>> is omitted, it defaults to ''1''.
Negative integers or zero are invalid.
<dt><dfn id='grid-placement-auto'>auto</dfn>
The property contributes nothing to the <a>grid item</a>’s <a>placement</a>,
indicating <a>auto-placement</a> or a default span of one.
(See [[#placement]], above.)
In all the above productions,
the <<custom-ident>> additionally excludes the keywords <css>span</css> and <css>auto</css>.
<div class='example'>
Given a single-row, 8-column grid and the following 9 named lines:
1 2 3 4 5 6 7 8 9
| | | | | | | | |
| | | | | | | | |
The following declarations place the grid item between the lines indicated by index:
grid-column-start: 4; grid-column-end: auto;
/* Line 4 to line 5 */
grid-column-start: auto; grid-column-end: 6;
/* Line 5 to line 6 */
grid-column-start: C; grid-column-end: C -1;
/* Line 3 to line 9 */
grid-column-start: C; grid-column-end: span C;
/* Line 3 to line 6 */
grid-column-start: span C; grid-column-end: C -1;
/* Line 6 to line 9 */
grid-column-start: span C; grid-column-end: span C;
/* Error: The end span is ignored, and an auto-placed
item can't span to a named line.
Equivalent to ''grid-column: span 1;''. */
grid-column-start: 5; grid-column-end: C -1;
/* Line 5 to line 9 */
grid-column-start: 5; grid-column-end: span C;
/* Line 5 to line 6 */
grid-column-start: 8; grid-column-end: 8;
/* Error: line 8 to line 9 */
grid-column-start: B 2; grid-column-end: span 1;
/* Line 5 to line 6 */
<h4 id='grid-placement-errors'>
Grid Placement Conflict Handling</h4>
If the <a>placement</a> for a <a>grid item</a> contains two lines,
and the <a>start</a> line is further end-ward than the <a>end</a> line,
swap the two lines.
If the <a>start</a> line is <em>equal</em> to the <a>end</a> line,
remove the <a>end</a> line.
If the <a>placement</a> contains two spans,
remove the one contributed by the <a>end</a> <a>grid-placement property</a>.
If the <a>placement</a> contains only a span for a named line,
replace it with a span of 1.
██████ ████████ ████ ████████ ██████ ███████ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ███ ███ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ████ ██
██ ████ ████████ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███
██████ ██ ██ ████ ████████ ██████ ███████ ████████ ███████ ██ ██ ██ ██
<h3 id='placement-shorthands'>
Placement Shorthands: the 'grid-column', 'grid-row', and 'grid-area' properties</h3>
<pre class='propdef shorthand'>
Name: grid-row, grid-column
Value: <<grid-line>> [ / <<grid-line>> ]?
Initial: auto
Applies to: <a>grid items</a> and absolutely-positioned boxes whose containing block is a <a>grid container</a>
Inherited: no
Percentages: N/A
Animation type: discrete
The 'grid-row' and 'grid-column' properties are shorthands for 'grid-row-start'/'grid-row-end' and 'grid-column-start'/'grid-column-end', respectively.
If two <<grid-line>> values are specified,
the 'grid-row-start'/'grid-column-start' longhand is set to the value before the slash,
and the 'grid-row-end'/'grid-column-end' longhand is set to the value after the slash.
When the second value is omitted,
if the first value is a <<custom-ident>>,
the 'grid-row-end'/'grid-column-end' longhand is also set to that <<custom-ident>>;
otherwise, it is set to <a value for="<grid-line>">auto</a>.
<pre class='propdef shorthand'>
Name: grid-area
Value: <<grid-line>> [ / <<grid-line>> ]{0,3}
Initial: auto
Applies to: <a>grid items</a> and absolutely-positioned boxes whose containing block is a <a>grid container</a>
Inherited: no
Percentages: N/A
Animation type: discrete
The 'grid-area' property is a <a>shorthand</a> for
'grid-row-start', 'grid-column-start', 'grid-row-end' and 'grid-column-end'.
If four <<grid-line>> values are specified,
'grid-row-start' is set to the first value,
'grid-column-start' is set to the second value,
'grid-row-end' is set to the third value,
and 'grid-column-end' is set to the fourth value.
When 'grid-column-end' is omitted,
if 'grid-column-start' is a <<custom-ident>>,
'grid-column-end' is set to that <<custom-ident>>;
otherwise, it is set to <a value for="<grid-line>">auto</a>.
When 'grid-row-end' is omitted,
if 'grid-row-start' is a <<custom-ident>>,
'grid-row-end' is set to that <<custom-ident>>;
otherwise, it is set to <a value for="<grid-line>">auto</a>.
When 'grid-column-start' is omitted,
if 'grid-row-start' is a <<custom-ident>>,
all four longhands are set to that value.
Otherwise, it is set to <a value for="<grid-line>">auto</a>.
Note: The resolution order for this shorthand is row-start/column-start/row-end/column-end,
which goes <abbr title="counterclockwise">CCW</abbr> for <abbr title="left-to-right">LTR</abbr> pages,
the opposite direction of the related 4-edge properties using physical directions, like 'margin'.
███ ██ ██ ████████ ███████ ████████ ██ ███ ██████ ████████ ██ ██ ████████ ██ ██ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ███ ██ ███ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ███████ ████████ ██ ██ ██ ██ ██████ ██ ███ ██ ██████ ██ ██ ██ ██
█████████ ██ ██ ██ ██ ██ ██ ██ █████████ ██ ██ ██ ██ ██ ██ ████ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██
██ ██ ███████ ██ ███████ ██ ████████ ██ ██ ██████ ████████ ██ ██ ████████ ██ ██ ██
<h3 id='auto-placement-algo'>
Grid Item Placement Algorithm</h3>
The following <dfn export local-lt="auto-placement algorithm">grid item placement algorithm</dfn>
resolves <a>automatic positions</a> of <a>grid items</a> into <a>definite positions</a>,
ensuring that every <a>grid item</a> has a well-defined <a>grid area</a> to lay out into.
(<a>Grid spans</a> need no special resolution;
if they're not explicitly specified,
they default to 1.)
Note: This algorithm can result in the creation of new rows or columns in the <a>implicit grid</a>,
if there is no room in the <a>explicit grid</a> to place an auto-positioned <a>grid item</a>.
Every <a>grid cell</a>
(in both the <a>explicit</a> and <a>implicit grids</a>)
can be <dfn>occupied</dfn> or <dfn>unoccupied</dfn>.
A cell is <a>occupied</a>
if it's covered by the <a>grid area</a>
of a <a>grid item</a> with a <a>definite grid position</a>;
the cell is <a>unoccupied</a>.
A cell's <a>occupied</a>/<a>unoccupied</a> status can change during this algorithm.
To aid in clarity,
this algorithm is written with the assumption that 'grid-auto-flow'
has ''grid-auto-flow/row'' specified.
If it is instead set to ''grid-auto-flow/column'',
swap all mentions of rows and columns, inline and block, etc. in this algorithm.
Note: The <a>auto-placement algorithm</a> works with the <a>grid items</a> in <a>order-modified document order</a>,
not their original document order.
0. <strong>Generate anonymous grid items</strong>
as described in [[#grid-items]].
(Anonymous <a>grid items</a> are always auto-placed,
since their boxes can't have any <a>grid-placement properties</a> specified.)
1. <strong>Position anything that's not auto-positioned.</strong>
2. <strong>Process the items locked to a given row.</strong>
For each <a>grid item</a> with a <a>definite row position</a>
(that is, the 'grid-row-start' and 'grid-row-end' properties define a <a>definite grid position</a>),
in <a>order-modified document order</a>:
: “sparse” packing (default behavior)
Set the column-start line of its <a>placement</a>
to the earliest (smallest positive index) line index
that ensures this item’s <a>grid area</a> will not overlap any <a>occupied</a> grid cells
and that is past any <a>grid items</a> previously placed in this row by this step.
: “dense” packing (''dense'' specified)
Set the column-start line of its <a>placement</a>
to the earliest (smallest positive index) line index
that ensures this item’s <a>grid area</a> will not overlap any <a>occupied</a> grid cells.
3. <strong>Determine the columns in the implicit grid.</strong>
Create columns in the <a>implicit grid</a>:
1. Start with the columns from the <a>explicit grid</a>.
2. Among all the items with a <a>definite column position</a>
(explicitly positioned items, items positioned in the previous step, and items not yet positioned but with a definite column)
add columns to the beginning and end of the <a>implicit grid</a>
as necessary to accommodate those items.
3. If the largest <a>column span</a> among all the items <em>without</em> a <a>definite column position</a>
is larger than the width of the <a>implicit grid</a>,
add columns to the end of the <a>implicit grid</a> to accommodate that <a>column span</a>.
<div class='example'>
For example, in the following style fragment:
#grid {
display: grid;
grid-template-columns: repeat(5, 100px);
grid-auto-flow: row;
#grid-item {
grid-column: 4 / span 3;