Copyright © 2014 W3C® (MIT, ERCIM, Keio, Beihang), All Rights Reserved. W3C liability, trademark and document use rules apply.
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 flexible or fixed predefined layout grid. CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, etc.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css-grid” in the subject, preferably like this: “[css-grid] …summary of comment…”
This document was produced by the CSS Working Group (part of the Style Activity).
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
The following features are at-risk, and may be dropped during the CR period:
Grid layout contains features targeted at web application authors. The grid can be used to achieve many different layouts. It excels at dividing up space for major regions of an application, or defining the relationship in terms of size, position, and layer between parts of a control built from HTML primitives.
Like tables, grid layout enables an author to align elements into columns and rows, but unlike tables, grid layout doesn’t have content structure, and thus enables a wide variety of layouts not possible with tables. For example, the children of a grid container can position themselves such that they overlap and layer similar to positioned elements.
In addition, the absence of content structure in grid layout helps to manage changes to layout by using fluid and source order independent layout techniques. By combining media queries 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, without needing to alter the semantic nature of their content.
As websites evolved from simple documents into complex, interactive applications, tools 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 by into grid areas defined by these columns and rows. Figure 1 illustrates a basic layout which can be achieved with grid layout.
Grid layout can be used to intelligently reflow elements within a webpage. Figure 2 represents a game with five major areas 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:
As an alternative to using script to control the absolute position, width, and height of all elements, the author can use grid layout, as shown in Figure 3. The following example shows how an author might achieve all the sizing, placement, and alignment rules declaratively.
Note that there are multiple ways to specify the structure of the grid and to position and size grid items, each optimized for different scenarios. This example illustrates one that an author may use to define the position and space for each grid item using the grid-template-rows and grid-template-columns properties on the grid container, and the grid-row and grid-column properties on each grid item.
<style type="text/css"> #grid { display: grid; /* Two columns: the first sized to content, 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. */ grid-template-columns: auto minmax(min-content, 1fr); /* Three rows: the first and last sized to content, the middle * row receives the remaining space, but is never smaller than * the minimum height of the board or stats areas. */ grid-template-rows: auto minmax(min-content, 1fr) auto } /* Each part of the game is positioned between grid lines by * referencing the starting grid line and then specifying, if more * than one, the number of rows or columns spanned to determine * the ending grid line, which establishes bounds for the part. */ #title { grid-column: 1; grid-row: 1 } #score { grid-column: 1; grid-row: 3 } #stats { grid-column: 1; grid-row: 2; justify-self: start } #board { grid-column: 2; grid-row: 1 / span 2; } #controls { grid-column: 2; grid-row: 3; align-self: center } </style> <div id="grid"> <div id="title">Game Title</div> <div id="score">Score</div> <div id="stats">Stats</div> <div id="board">Board</div> <div id="controls">Controls</div> </div>
Continuing the prior example, the author also wants the game to adapt to the space available on traditional computer monitors, handheld devices, or tablet computers. Also, the game should optimize the placement of the components when viewed either in landscape or portrait orientation (Figures 4 and 5). 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 leverages grid layout’s ability to name the space which will be occupied by a grid item. This allows the author to avoid rewriting rules for grid items as the grid’s definition changes.
<style type="text/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"; /* Columns and rows created with the template property can be * assigned a sizing function with the grid-template-columns * and grid-template-rows properties. */ grid-template-columns: auto minmax(min-content, 1fr); grid-template-rows: auto auto minmax(min-content, 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 minmax(min-content, 1fr); grid-template-rows: auto minmax(min-content, 1fr) auto } } /* The grid-area property places a grid item into a named * region (area) of the grid. */ #title { grid-area: title } #score { grid-area: score } #stats { grid-area: stats } #board { grid-area: board } #controls { grid-area: ctrls } </style> <div id="grid"> <div id="title">Game Title</div> <div id="score">Score</div> <div id="stats">Stats</div> <div id="board">Board</div> <div id="controls">Controls</div> </div>
In the example shown in Figure 6, the author is creating a custom slider control. The control has six parts. The lower and upper labels align to the left and right edges of the control. The track of the slider spans the area between the labels. The lower and upper fill parts touch beneath the thumb, and the thumb is a fixed width and height that can be moved along the track by updating the two flex-sized columns.
Prior to the introduction of grid layout, the author would have likely used absolute positioning to control the top and left coordinates, along with the width and height of each HTML element that comprises the control. By leveraging grid layout, the author can instead limit script usage to handling mouse events on the thumb, which snaps to various positions along the track as the grid-template-columns property of the grid container is updated.
<style type="text/css"> #grid { display: grid; /* The grid-template-columns and rows properties also support * naming grid lines which can then be used to position grid * items. The line names are assigned on either side of a column * or row sizing function where the line would logically exist. */ grid-template-columns: (start) auto (track-start) 0.5fr (thumb-start) auto (fill-split) auto (thumb-end) 0.5fr (track-end) auto (end); } /* The grid-placement properties accept named lines. Below the * lines are referred to by name. Beyond any * semantic advantage, the names also allow the author to avoid * renumbering the grid-column-start and grid-row-start properties of the * grid items. This is similar to the concept demonstrated in the * prior example with the grid-template-areas property during orientation * changes, but grid lines can also work with layered grid items * that have overlapping areas of different shapes like the thumb * and track parts in this example. */ #lower-label { grid-column-start: start } #track { grid-column: track-start / track-end; align-self: center } #upper-label { grid-column-end: end; } /* Fill parts are drawn above the track so set z-index to 5. */ #lower-fill { grid-column: track-start / fill-split; align-self: end; z-index: 5 } #upper-fill { grid-column: fill-split / track-end; align-self: start; z-index: 5 } /* Thumb is the topmost part; assign it the highest z-index value. */ #thumb { grid-column: thumb-start / thumb-end; z-index: 10 } </style> <div id="grid"> <div id="lower-label">Lower Label</div> <div id="upper-label">Upper Label</div> <div id="track">Track</div> <div id="lower-fill">Lower Fill</div> <div id="upper-fill">Upper Fill</div> <div id="thumb">Thumb</div> </div>
In grid layout, the content of a grid container is laid out by positioning and aligning it into a grid. The grid is an intersecting set of horizontal and vertical grid lines that divides the grid container’s space into grid areas, into which grid items (representing the grid container’s content) can be placed. There are two sets of grid lines: one set defining columns that run along the block axis (the column axis), and an orthogonal set defining rows along the inline axis (the row axis). [CSS3-WRITING-MODES]
Grid track is a generic term for a grid column or grid row—in other words, it is the space between two adjacent grid lines. Each grid track is assigned a sizing function, which controls how wide or tall the column or row may grow, and thus how far apart its bounding grid lines are.
A grid cell is the similar term for the full grid—it is the space between two adjacent row and two adjacent column grid lines. It is the smallest unit of the grid that can be referenced when positioning grid items.
<style type="text/css"> #grid { display: grid; grid-template-columns: 150px 1fr; /* two columns */ grid-template-rows: 50px 1fr 50px /* three rows */ } </style>
Grid lines are the horizontal and vertical dividing lines of the grid. A grid line exists on either side of a column or row. They can be referred to by numerical index, or by an author-specified name. A grid item references the grid lines to determine its position within the grid using the grid-placement properties.
<style type="text/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; } </style>
<style type="text/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 } </style>
A grid area is the logical space used to lay out one or more grid items. It is bound by four grid lines, one on each side of the grid area, and participates in the sizing of the grid tracks it intersects. A grid area can be named explicitly using the grid-template-areas property of the grid container, or referenced implicitly by its bounding grid lines. A grid item is assigned to a grid area using the grid-placement properties.
<style type="text/css"> /* 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: head } #item3 { justify-self: end; align-self: foot } </style>
A grid item’s grid area forms the containing block into which it is laid out. Percentage lengths specified on a grid item resolve against this containing block. Percentages specified for margin-top, padding-top, margin-bottom, and padding-bottom on a grid item resolve against the height of its containing block, rather than the width (as for blocks).
Grid items placed into the same grid area do not directly affect each other’s layout. Indirectly, a grid item can affect the position of a grid line in a column or row that uses a contents-based relative size, which in turn can affect the position or size of another grid item.
Name: | display |
---|---|
New values: | grid | inline-grid |
A grid container establishes a new grid formatting context for its contents. This is the same as establishing a 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. Grid containers form a containing block for their contents exactly like block containers do. [CSS21] The overflow property applies to grid containers.
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:
If an element’s specified display is inline-grid and the element is floated or absolutely positioned, the computed value of display is grid. The table in CSS 2.1 Chapter 9.7 is thus amended to contain an additional row, with inline-grid in the "Specified Value" column and grid in the "Computed Value" column.
A grid container is sized using the rules of the formatting context in which it participates. As a block-level box in a block formatting context, it is sized like any other block-level box that establishes a formatting context, with an auto inline size calculated as for in-flow block boxes. As an inline-level box in an inline formatting context, it is sized as an atomic inline-level box (such as an inline-block). In both inline and block formatting contexts, the grid container’s auto block size is its max-content size. The block layout spec should define this?
The max-content size of a grid container is the sum of the grid container’s track sizes in the appropriate axis, when the grid is sized under a max-content constraint.
The min-content size of a grid container is the sum of the grid container’s track sizes in the appropriate axis, when the grid is sized under a min-content constraint.
See [CSS3-SIZING] for a definition of the terms in this section.
The contents of a grid container consists of zero or more grid items: each child of a grid container becomes a grid item, and each contiguous run of text that is directly contained inside a grid container is wrapped in an anonymous grid item. However, an anonymous grid item that contains only white space is not rendered, as if it were display:none.
A grid item establishes a new formatting context for its contents. The type of this formatting context is determined by its display value, as usual. The computed display of a grid item is determined by applying the table in CSS 2.1 Chapter 9.7. However, grid items are grid-level boxes, not block-level boxes: they participate in their container’s grid formatting context, not in a block formatting context.
<div style="display:grid"> <!-- grid item: block child --> <div id="item1">block</div> <!-- grid item: floated element; floating is ignored --> <div id="item2" style="float: left;">float</div> <!-- grid item: anonymous block box around inline content --> anonymous item 3 <!-- grid item: inline child --> <span> item 4 <!-- grid items do not split around blocks --> <div id=not-an-item>item 4</div> item 4 </span> </div>
Some values of display trigger the generation of anonymous boxes. For example, a misparented table-cell child is fixed up by generating anonymous table and table-row elements around it. [CSS21] This fixup must occur before a grid container’s children are promoted to grid items. For example, given two contiguous child elements with display:table-cell, an anonymous table wrapper box around them becomes the grid item.
Future display types may generate anonymous containers (e.g. ruby) or otherwise mangle the box tree (e.g. run-ins). It is intended that grid item determination run after these operations.
We want the ability to collapse grid tracks (similar to collapsing flex items or table rows/columns), but we’re not sure exactly how to do it. Ideas welcome, please post them to www-style@w3.org.
The order property also applies to grid items. It affects their auto-placement and painting order.
The static position [CSS21] of an absolutely-positioned child of a grid container is determined as if it were the sole grid item in a fixed-size grid area whose edges coincide with the padding edges of the grid container.
Note: Note that this position is affected by the values of justify-self and align-self on the child, and that, as in most other layout models, the absolutely-positioned child has no effect on the size of the containing block or layout of its contents.
The three properties grid-template-rows, grid-template-columns, and grid-template-areas together define the explicit grid of a grid container. The grid-template property is a shorthand that sets all three at the same time. The final grid may end up larger due to grid items placed outside the explicit grid; in this case, any implicit tracks are sized by the grid-auto-rows and grid-auto-columns properties.
The size of the explicit grid 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.
Numeric indexes in the grid-placement properties count from the edges of the explicit grid, starting from 1. Positive indexes count from the before/start side, while negative indexes count from the after/end side.
Name: | grid-template-columns, grid-template-rows |
---|---|
Value: | none | "><track-list> | subgrid "><line-name-list>? |
Initial: | none |
Applies to: | grid containers |
Inherited: | no |
Media: | visual |
Computed value: | As specified, except for auto (see prose), with lengths made absolute |
Percentages: | refer to corresponding dimension of the content area |
These properties specify, as a space-separated track list, the line names and track sizing functions of the grid. Each track sizing function can be specified as a length, a percentage of the grid container’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 min and max track sizing functions for the column or row.
The grid-template-columns property specifies the track list for the grid’s columns, while grid-template-rows specifies the track list for the grid’s rows. The syntax of a track list is:
<track-list> = [ "><line-names>? [ "><track-size> | <repeat()> ] ]+ "><line-names>? <track-size> = minmax( "><track-breadth> , "><track-breadth> ) | auto | "><track-breadth> <track-breadth> = "><length> | "><percentage> | "><flex> | min-content | max-content <line-names> = ( "><custom-ident>* ) <line-name-list> = [ "><line-names> | repeat("><positive-integer>, "><line-names>+) ]+
Where:
The none value indicates that there is no explicit grid; any rows/columns will be implicitly generated, and their size will be determined by the grid-auto-rows and grid-auto-columns properties.
The subgrid value indicates that the grid will align to its parent grid in that axis. Rather than specifying the sizes of rows/columns explicitly, they’ll be taken from the parent grid’s definition.
grid-template-columns: 100px 1fr max-content minmax(min-content, 1fr);
Five grid lines are created:
If the non-flexible sizes (100px, max-content, and min-content) sum to larger than the grid container’s width, the final grid line will be a distance equal to their sum away from the start edge of the grid container (the 1fr sizes both resolve to 0). If the sum is less than the grid container’s width, the final grid line will be exactly at the end edge of the grid container. This is true in general whenever there’s at least one "><flex> value among the grid track sizes.
/* 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)
While grid lines can always be referred to by their numerical index, named lines can make the grid-placement properties easier to understand and maintain. Lines can be explicitly named in the grid-template-rows and grid-template-columns properties, or implicitly named by creating named grid areas with the grid-template-areas property.
<style> #grid { display: grid; grid-template-columns: (first nav) 150px (main) 1fr (last); grid-template-rows: (first header) 50px (main) 1fr (footer) 50px (last); } </style>
The repeat() notation represents a repeated fragment of the track list. This is just a syntactic shorthand that allows writing a large number of columns or rows that exhibit a recurring pattern in a more compact form. The syntax of the repeat() notation is:
repeat() = repeat( "><positive-integer> , [ "><line-names>? "><track-size> ]+ "><line-names>? )
The first argument specifies the number of repetitions. The second argument is a track list, which is repeated that number of times. The repeat() notation cannot be nested; doing so makes the declaration invalid.
<style> #grid { display: grid; 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; grid-template-rows: 1fr; } /* Equivalent definition. */ #grid { display: grid; grid-template-columns: repeat(4, 10px (col-start) 250px (col-end)) 10px; grid-template-rows: 1fr; } </style>
If the 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).
Note: It is expected that a future level of this specification will define a value to be inserted between the repetitions,
similar to the argument of the Array#join
method in JavaScript.
Implementations may cap the maximum number of repetitions they allow. If they do so, they must support at least 10000 repetitions, and if the number of repetitions is greater than the implementation’s maximum, the value must be capped to the implementation’s maximum (rather than being invalid, overflowing, or some other behavior).
A flexible length or "><flex> is a dimension with the fr unit, which represents a fraction of the free space in the grid container.
The distribution of free space occurs after all non-flexible track sizing functions have reached their maximum. The total size of such rows or columns is subtracted from the available space, yielding the free space, which is then divided among the flex-sized rows and columns in proportion to their flex factor.
Note: Flexible lengths in a track list work similarly to flexible lengths with a zero base size in [CSS3-FLEXBOX].
Each column or row’s share of the free space can be computed as the column or row’s
<flex> * <free space> / <sum of all flex factors
.
For the purpose of this calculation,
a flexible length in the min position of a minmax() function is treated as 0 (an inflexible length).
When the available space is infinite (which happens when the grid container’s width or height is indefinite), flex-sized grid tracks are sized to their contents while retaining their respective proportions. The used size of each flex-sized grid track is computed by determining the max-content size of each flex-sized grid track and dividing that size by the respective flex factor to determine a “hypothetical 1fr size”. The maximum of those is used as the resolved 1fr length (the flex fraction), which is then multiplied by each grid track’s flex factor to determine its final size.
A grid item can itself be a grid container by giving it display: grid; in this case the layout of its contents will be independent of the layout of the grid it participates in.
In some cases it might be necessary for the contents of multiple grid items to align to each other. A grid container that is itself a grid item can defer the definition of its rows or columns to its parent grid container by using the subgrid keyword in grid-template-rows and/or grid-template-columns, making it a subgrid in that dimension. In this case, the grid items of the subgrid participate in sizing the grid of the parent grid container, allowing the contents of both grids to align.
<ul> <li><label>Name:</label> <input name=fn> <li><label>Address:</label> <input name=address> <li><label>Phone:</label> <input name=phone> </ul>
We want the labels and inputs to align, and we want to style each list item with a border. This can be accomplished with subgrid layout:
ul { display: grid; grid-auto-flow: row; grid-template-columns: auto 1fr; } li { display: grid; grid: subgrid; margin: 0.5em; border: solid; padding: 0.5em; } label { grid-column: 1; } input { grid-column: 2; }
A subgrid behaves just like a normal grid container except that:
#parent-grid { grid-template-columns: 300px auto 300px; }
If a subgrid covers the last two tracks, its first two columns correspond to the parent grid’s last two columns, and any items positioned into those tracks participate in sizing the parent grid. Specifically, an item positioned in the first track of the subgrid influences the auto-sizing of the parent grid’s middle track.
#subgrid { grid-column: 2 / span 2; } /* cover parent’s 2nd and 3rd tracks */ #subgrid :first-child { grid-column: 1; } /* subgrid’s 1st track, parent grid’s 2nd track */
If the subgrid has margins/borders/padding, the the size of those margins/borders/padding also influence sizing. For example, if the subgrid has 100px padding:
#subgrid { padding: 100px; }
Then when the parent grid auto-sizes its second track, it will be at least 100px wider than any items in the subgrid’s first track, and any items in the subgrid’s second track will be sized to fit a slot 200px wide (instead of 300px wide).
However, any overflow tracks (i.e. those outside the explicit grid when the subgrid has a definite grid span) do not correspond to any tracks in the parent grid; they effectively extend in a third dimension.
#subgrid { grid-template-columns: subgrid (first) (second) (third) (fourth) (fifth); }
Items within the subgrid could be positioned using the first four line names; the last name would be ignored (as if it didn’t exist), since the subgrid only covers four lines.
If the subgrid has an explicit grid position as well as an explicit grid span, it also automatically receives the line names specified for its parent grid. (In such cases the author can rely on the names specified in the parent grid, and does not need to duplicate those names in each subgrid declaration.)
When an element’s display is grid or inline-grid and it generates a box, the resolved value of the grid-template-rows and grid-template-columns properties is the used value, serialized as follows:
Otherwise, (e.g. when the element has display: none or is not a grid) the resolved value is simply the computed value.
<style> #grid { width: 500px; grid-template-columns: (a) auto (b) minmax(min-content, 1fr) (b c d) repeat(2, (e) 40px) repeat(5, auto); } </style> <div id="grid"> <div style="grid-column-start:1; width:50px"></div> <div style="grid-column-start:9; width:50px"></div> </div> <script> var gridElement = document.getElementById("grid"); getComputedStyle(gridElement).gridDefinitionColumns; // (a) 50px (b) 320px (b c d) repeat(2, (e) 40px) repeat(4, 0px) 50px </script>
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.
Authors are recommended to use the
.rawComputedStyle
and .usedStyle
attributes
instead of getComputedStyle()
.
Name: | grid-template-areas |
---|---|
Value: | none | "><string>+ |
Initial: | none |
Applies to: | grid containers |
Inherited: | no |
Media: | visual |
Computed value: | specified value |
Percentages: | n/a |
This property specifies named grid areas, which are not associated with any particular grid item, but can be referenced from the grid-placement properties. The syntax of the grid-template-areas property also provides a visualization of the structure of the grid, making the overall layout of the grid container easier to understand.
Values have the following meanings:
Tokenize the string into a list of the following tokens, using longest-match semantics:
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.
All strings must have the same number of columns, or else the declaration is invalid. If a named grid area spans multiple grid cells, 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.
head
),
navigational content (nav
),
footer content (foot
),
and main content (main
).
Accordingly, the template creates three rows and two columns,
with four named grid areas.
The head
area spans both columns and the first row of the grid.
<style type="text/css"> #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; } </style>
The grid-template-areas property creates implicit named lines from the named grid areas in the template. For each named grid area foo, four implicit named lines are created: two named foo-start, naming the row-start and column-start lines of the named grid area, and two named foo-end, naming the row-end and column-end lines of the named grid area.
These named lines behave just like any other named line, except that they do not appear in the value of grid-template-rows/grid-template-columns. Even if an explicit line of the same name is defined, the implicit named lines are just more lines with the same name.
Since a named grid area is referenced by the implicit named lines it produces, explicitly adding named lines of the same form (foo-start/foo-end) effectively creates a named grid area. Such implicit named areas do not appear in the value of grid-template-areas, but can still be referenced by the grid-placement properties.
Name: | grid-template |
---|---|
Value: | none | subgrid | <‘grid-template-columns’> / <‘grid-template-rows’> | [ "><track-list> / ]? [ "><line-names>? "><string> "><track-size>? "><line-names>? ]+ |
Initial: | see individual properties |
Applies to: | grid containers |
Inherited: | see individual properties |
Media: | visual |
Computed value: | see individual properties |
Percentages: | see individual properties |
The grid-template property is a shorthand for setting grid-template-columns, grid-template-rows, and grid-template-areas in a single declaration. It has several distinct syntax forms:
grid-template: auto 1fr auto / auto 1fr;
is equivalent to
grid-template-columns: auto 1fr auto; grid-template-rows: auto 1fr; grid-template-areas: none;
This syntax allows the author to align track names and sizes inline with their respective grid areas.
grid-template: auto 1fr auto / (header-top) "a a a" (header-bottom) (main-top) "b b b" 1fr (main-bottom);
is equivalent to
grid-template-columns: auto 1fr auto; grid-template-rows: (header-top) auto (header-bottom main-top) 1fr (main-bottom); grid-template-areas: "a a a" "b b b";
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.
While grid-template and its longhand properties define the explicit grid, grid items can be positioned outside of these bounds. This causes the grid container to generate implicit grid tracks, forming the implicit grid. The grid-auto-rows and grid-auto-columns properties size these implicit grid tracks.
The grid-auto-flow property controls auto-placement of grid items without an explicit position. Once the explicit grid is filled (or if there is no explicit grid) auto-placement will also cause the generation of implicit grid tracks.
Name: | grid-auto-columns, grid-auto-rows |
---|---|
Value: | "><track-size> |
Initial: | auto |
Applies to: | grid containers |
Inherited: | no |
Media: | visual |
Computed value: | see Track Sizing |
Percentages: | see Track Sizing |
If a grid item is positioned into a row or column that is not explicitly sized by grid-template-rows or grid-template-columns, 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. The grid-auto-columns and grid-auto-rows properties specify the size of such implicitly-created tracks.
<style type="text/css"> #grid { display: grid; grid-template-columns: 20px; grid-template-rows: 20px } #A { grid-column: 1; grid-row: 1; } #B { grid-column: 5; grid-row: 1 / span 2; } #C { grid-column: 1 / span 2; grid-row: 2; } </style> <div id="grid"> <div id="A">A</div> <div id="B">B</div> <div id="C">C</div> </div>
Name: | grid-auto-flow |
---|---|
Value: | [ row | column ] && dense? | stack && [ row | column ]? |
Initial: | row |
Applies to: | grid containers |
Inherited: | no |
Media: | visual |
Computed value: | specified value |
Percentages: | n/a |
Grid items that aren’t explicitly placed are automatically placed into an unoccupied space in the grid container. The grid-auto-flow property controls the direction in which the search for unoccupied space takes place, and whether rows or columns are added as needed to accommodate the content.
Note: This may cause items to appear out-of-order.
Wherever the hypothetical item would be placed, all auto-placed items are placed there, stacked atop one another. (This happens even if the auto-placed item is greater than 1×1, even if doing so would cause it to overlap explicitly-placed grid 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 grid items in order-modified document order.
A form arranged using automatic placement.
<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; } 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 { /* 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 row axis. */ grid-column: 1 / -1; text-align: end; } </style> <form> <label for="firstname">First name:</label> <input type="text" id="firstname" name="firstname" /> <label for="lastname">Last name:</label> <input type="text" id="lastname" name="lastname" /> <label for="address">Address:</label> <input type="text" id="address" name="address" /> <label for="address2">Address 2:</label> <input type="text" id="address2" name="address2" /> <label for="city">City:</label> <input type="text" id="city" name="city" /> <label for="state">State:</label> <select type="text" id="state" name="state"> <option value="WA">Washington</option> </select> <label for="zip">Zip:</label> <input type="text" id="zip" name="zip" /> <div id="department"> <label for="department">Department:</label> <select id="department" name="department" multiple> <option value="finance">Finance</option> <option value="humanresources">Human Resources</option> <option value="marketing">Marketing</option> </select> </div> <div id="buttons"> <button id="cancel">Cancel</button> <button id="back">Back</button> <button id="next">Next</button> </div> </form>
The following auto-placement algorithm places grid items with automatic grid positions into a definite grid position.
To aid in clarity, this algorithm is written with the assumption that grid-auto-flow is set to row. If it is instead set to column, swap all mentions of rows and columns in this algorithm.
For the purpose of this algorithm, an occupied grid cell is any grid cell which is contained within any named grid area or the grid area of any previously-positioned grid item.
Before auto-positioning anything, position every grid item with a definite grid position in both axes.
For each grid item with a definite row position (that is, the grid-row-start and grid-row-end properties define a definite grid position), in order-modified document order, position its inline-start edge to the earliest (smallest positive index) line index that ensures this item’s grid area will not overlap any occupied grid cells.
Set the number of columns in the implicit grid to the larger of:
#grid { display: grid; grid-template-columns: repeat(5, 100px); grid-auto-flow: row; } #grid-item { grid-column: 4 / span 3; }
The number of columns needed is 6.
The #grid-item
element’s inline-start edge is positioned at index 4,
so its span ensures that it’s inline-end edge will be positioned at index 7.
This requires 7 - 1 = 6
columns to hold,
which is larger than the explicit grid’s 5 columns.
The auto-placement cursor defines the current "insertion point" in the grid, specified as a pair of row and column grid lines. Initially the auto-placement cursor at is specified with a row and column position both equal to 1.
For each grid item that hasn’t been positioned by the previous steps, in order-modified document order:
If grid-auto-flow specifies the dense keyword, reset the auto-placement cursor to 1/1 each time an item is placed.
Review this wrt concerns in bug 16044.
Name: | grid |
---|---|
Value: | <‘grid-template’> | [ <‘grid-auto-flow’> [ <‘grid-auto-columns’> [ / <‘grid-auto-rows’> ]? ]? ] |
Initial: | see individual properties |
Applies to: | grid containers |
Inherited: | see individual properties |
Media: | visual |
Computed value: | see individual properties |
Percentages: | see individual properties |
The grid property is a shorthand that sets all of the explicit grid properties (grid-template-rows, grid-template-columns, and grid-template-areas) as well as all the implicit grid properties (grid-auto-rows, grid-auto-columns, and grid-auto-flow) in a single declaration. If <‘grid-auto-rows’> value is omitted, it is set to the value specified for grid-auto-columns. Other omitted values are set to their initial values.
Note: Note that you can only specify the explicit or 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 shorthands.
grid-template: none; grid-auto-columns: 1fr; grid-auto-rows: 1fr; grid-auto-flow: row;
Similarly, grid: columns 1fr / auto is equivalent to
grid-template: none; grid-auto-columns: 1fr; grid-auto-rows: auto; grid-auto-flow: column;
A grid item’s placement consists of a grid position and a grid span:
The grid-placement properties— A definite value for any two of Start, End, and Span 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 definite or automatic:
This section is informative.
The grid-placement property longhands are organized into three shorthands:
An item can be placed into a named grid area
(such as those produced by the template in grid-template-areas)
by specifying the area’s name in grid-area:
An item can also be partially aligned with a named grid area,
with other edges aligned to some other line:
Grid items can be positioned and sized by number,
which is particularly helpful for script-driven layouts:
By default, a grid item has a span of 1.
Different spans can be given explicitly:
Note: Note that grid indexes are writing mode relative.
For example, in a right-to-left language like Arabic,
the first column is the rightmost column.
Instead of counting lines by number,
named lines can be referenced by their name:
Note: Note that if a named grid area and a named line have the same name,
the placement algorithm will prefer to use named grid area’s edge 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:
A grid item can be automatically placed into the next available empty grid cell,
growing the grid if there’s no space left.
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:
Whether the auto-placement algorithm searchs across and adds rows,
or searches across and adds columns,
is controlled by the grid-auto-flow property.
Note: By default, the auto-placement algorithm 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.
A subgrid without a definite grid span is sized to its implicit grid,
i.e. all of its items within it are first placed into its own grid,
and its span is the resulting size of its grid.
If it contains 10 auto-placed items, it will span 5 rows in its parent grid.
Note: Since auto-spanning subgrids determine their span before being positioned,
they can also be auto-positioned.
The above example would auto-place the The grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
determine a grid item’s size and location within the grid
by contributing a line, a span, or nothing (automatic)
to its grid placement,
thereby specifying the inline-start, block-start, inline-end, and block-end edges of its grid area.
Values have the following meanings:
Note: Named grid areas automatically generate implicit named lines of this form,
so specifying grid-row-start: foo will choose the start edge of that named grid area
(unless another line named foo-start was explicitly specified before it).
Otherwise, if there is a named line with the specified name,
contributes the first such line to the grid item’s placement.
Otherwise,
the property contributes nothing (just as if " data-link-type=value href=#grid-placement-auto title=auto>auto were specified).
If a name is given as a "><custom-ident>,
only lines with that name are counted.
(If no line with that name exists,
it instead specifies the first grid line;
or the last, if the "><integer> is negative.
If not enough lines of that name exist,
it specifies the last such named line;
or the first, if the "><integer> is negative.)
A "><integer> value of zero makes the declaration invalid.
If a name is given as a "><custom-ident>,
only lines with that name are counted.
(If no line with that name exists,
the name is ignored.
If not enough lines of that name exist,
it spans to the last such named line in the spanning direction.)
If the "><integer> is omitted, it defaults to 1.
Negative integers or zero are invalid.
The following declarations place the grid item between the lines indicated by index:
If grid-row-end/grid-column-end specifies a line at or before that specified by grid-row-start/grid-column-start,
it instead contributes nothing.
If both grid-row-start and grid-row-end, or grid-column-start and grid-column-end,
specify a span,
the end span is ignored.
If the grid item has an automatic position
and a grid span for a named line
in a given dimension,
instead treat the grid span as one.
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 " data-link-type=value href=#grid-placement-auto title=auto>auto.
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 " data-link-type=value href=#grid-placement-auto title=auto>auto.
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 " data-link-type=value href=#grid-placement-auto title=auto>auto.
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 " data-link-type=value href=#grid-placement-auto title=auto>auto.
Note: The resolution order for this shorthand is row-start/column-start/row-end/column-end,
which goes CCW for LTR pages,
the opposite direction of the related 4-edge properties using physical directions, like margin.
If an absolutely positioned element’s containing block
is generated by a grid container,
its containing block edges are the edges of the element’s grid area
as given by its grid-placement properties.
In this case, an " data-link-type=value href=#grid-placement-auto title=auto>auto value for a grid-placement property
contributes the corresponding padding edge of the grid container as a line.
(Thus, by default, the containing block will correspond to the padding edges of the grid container.)
The offset properties (top/right/bottom/left)
then indicate offsets inwards from the corresponding edges
of the resulting containing block, as normal.
Note: Note that, while absolutely-positioning an element to a grid container
does allow it to align to that container’s grid lines,
such elements
do not take up space or otherwise participate in the layout of the grid.
Note: Note that grids and the grid-placement properties are flow-relative,
while abspos offsets are physical,
so if the direction or writing-mode properties change,
the grid will transform to match,
but the offsets won’t.
After a grid container’s grid tracks have been sized,
and the dimensions of all grid items are finalized,
grid items can be aligned within their grid areas.
The margin properties can be used to align items in a manner similar to,
but more powerful than,
what margins can do in block layout.
Grid items also respect the alignment properties from the Box Alignment spec,
which allow easy keyword-based alignment of items in both the row axis and column axis.
By default,
grid items stretch to fill their grid area.
However, if justify-self or align-self compute to a value other than stretch
or margins are auto,
grid items will auto-size to fit their contents.
This section is non-normative.
The normative definition of how margins affect grid items is in §10
Track Sizing Algorithm.
Auto margins on grid items have an effect very similar to auto margins in block flow:
Note: Note that, if free space is distributed to auto margins,
the alignment properties will have no effect in that dimension
because the margins will have stolen all the free space
left over after sizing.
Grid items can be aligned in the inline dimension
by using the justify-self property on the grid item
or justify-items property on the grid container,
as defined in [CSS3-ALIGN].
Grid items can also be aligned in the block dimension
(perpendicular to the inline dimension)
by using the align-self property on the grid item
or align-items property on the grid container,
as defined in [CSS3-ALIGN].
If the grid’s outer edges do not correspond to the grid container’s content edges
(for example, if no columns are flex-sized),
the grid is aligned within the content box according to the justify-content and align-content properties on the grid container.
Grid items can overlap when they are positioned into intersecting grid areas,
or even when positioned in non-intersecting areas because of negative margins or positioning.
The painting order of grid items is exactly the same as inline blocks [CSS21],
except that order-modified document order is used in place of raw document order,
and z-index values other than auto create a stacking context even if position is static.
Thus the z-index property can easily be used to control the z-axis order of grid items.
Note: Descendants that are positioned outside a grid item still participate in any stacking context established by the grid item.
The baselines of a grid container are determined as follows:
A grid item participates in baseline alignment in a particular dimension
if its value for align-self or justify-self, as appropriate, is baseline
and its inline axis is parallel to that dimension.
order-modified grid order is the order in which
grid items are encountered when traversing the grid’s grid cells,
in row-major order if calculating the inline-axis baseline,
or in column-major order if calculating the block-axis baseline.
If two items are encountered at the same time,
they are taken in order-modified document order.
When calculating the baseline according to the above rules,
if the box contributing a baseline has an overflow value that allows scrolling,
the box must be treated as being in its initial scroll position
for the purpose of determining its baseline.
When determining the baseline of a table cell,
a grid container provides a baseline just as a line box or table-row does. [CSS21]
If available space is indefinite,
the free space is indefinite as well.
Each track has specified minimum and
maximum sizing functions
(which may be the same).
Each sizing function is either:
The grid layout algorithm defines how to resolve these sizing constraints into used track sizes.
Once the size of each grid area is thus established,
the grid items are laid out into their respective containing blocks.
The remainder of this section is the track sizing algorithm,
which calculates from the min and max track sizing functions
the used track size.
Each track has a base size,
a "><length> which grows throughout the algorithm
and which will eventually be the track’s final size,
and a growth limit,
a "><length> which provides a desired maximum size for the base size.
(The base size might end up being larger than the growth limit,
but the algorithm attempts to avoid this situation.)
There are 4 steps:
Make sure orthogonal flows are correctly addressed.
(discussion)
Initialize each track’s base size and growth limit.
For each track,
if the track’s min track sizing function is:
A track with a flexible minimum sizing function
is treated exactly as if it had a fixed min track sizing function of zero;
except when the grid container is being sized under a min-content constraint,
in which case it is treated exactly as min-content.
This seems correct, but should be checked because it wasn’t in the original algorithm.
This step resolves intrinsic track sizing functions to absolute lengths.
First it resolves those sizes based on items that are contained wholly within a single track.
Then it gradually adds in the space requirements of items that span multiple tracks,
evenly distributing the extra space across those tracks
insofar as possible.
Note: There is no single way to satisfy these constraints
when items span across multiple tracks.
This algorithm embodies a number of heuristics
which have been seen to deliver good results on real-world use-cases,
such as the "game" examples earlier in this specification.
This algorithm may be updated in the future
to take into account more advanced heuristics as they are identified.
Repeat incrementally for items with greater spans until all items have been considered.
To distribute extra space
by increasing the affected sizes of a set of tracks
as required by a set of intrinsic size contributions,
If a track was marked as infinitely growable for this phase,
treat its growth limit as infinite for this calculation
(and then unmark it).
At this point, all intrinsic base sizes and growth limits
will have been resolved to absolute lengths.
If the free space is positive, distribute it equally to all tracks,
freezing tracks as they reach their growth limits
(and continuing to grow the unfrozen tracks as needed).
For the purpose of this step:
if sizing the grid container under a max-content constraint,
the free space is infinite;
if sizing under a min-content constraint,
the free space is zero.
This step sizes flexible tracks
using the largest value it can assign to an fr
without exceeding the available space.
First, find the used flex fraction:
For each flexible track,
if the product of the used flex fraction and the track’s flex factor
is greater than the track’s base size,
set its base size to that product.
This algorithm finds the largest size that an fr unit can be
without exceeding the target size.
It must be called with a set of grid tracks
and some quantity of space to fill.
This probably needs to be adjusted to handle sum(flex factors) approaches zero,
same as how Flexbox does it.
This specification is made possible by input from
Erik Anderson,
Rossen Atanassov,
Arron Eicholz,
Sylvain Galineau,
Markus Mielke,
John Jansen,
Chris Jones,
Kathy Kam,
Veljko Miljanic,
Peter Salas,
Christian Stockwell,
Eugene Veselov,
and the CSS Working Group members.
Thanks to Eliot Graff for editorial input.
The following significant changes were made since the
10 September 2013 Working Draft.
Note: The following is a more direct translation of the
original Microsoft editors' track sizing algorithm,
from which the current algorithm is derived.
It follows the original algorithm structure
without being as programmatic in its language.
It is preserved here to aid in review of the current specification
(and will be removed in the next revision).
This algorithm is run multiple times,
once for each set of grid items with a particular span count,
and each time it moves through four phases,
each one addressing one combination of min-content or max-content
as a min track sizing function or max track sizing function.
Starting from the set of all grid items in the grid,
remove any with a span count greater than one
which span a track with a "><flex> max track sizing function.
Of the remaining,
sort them into groups according to their span count,
and order the groups in ascending order of span count.
For each group, in order,
perform the following steps four times:
Let each item’s needed space be:
Let each item’s growable tracks be:
Let each item’s overgrowable tracks be:
For each grid item in the group:
Conformance requirements are expressed with a combination of
descriptive assertions and RFC 2119 terminology. The key words "MUST",
"MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT",
"RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this
document are to be interpreted as described in RFC 2119.
However, for readability, these words do not appear in all uppercase
letters in this specification.
All of the text of this specification is normative except sections
explicitly marked as non-normative, examples, and notes. [RFC2119] Examples in this specification are introduced with the words "for example"
or are set apart from the normative text with This is an example of an informative example. Informative notes begin with the word "Note" and are set apart from the
normative text with Note, this is an informative note. Conformance to this specification
is defined for three conformance classes:
A style sheet is conformant to this specification
if all of its statements that use syntax defined in this module are valid
according to the generic CSS grammar and the individual grammars of each
feature defined in this module.
A renderer is conformant to this specification
if, in addition to interpreting the style sheet as defined by the
appropriate specifications, it supports all the features defined
by this specification by parsing them correctly
and rendering the document accordingly. However, the inability of a
UA to correctly render a document due to limitations of the device
does not make the UA non-conformant. (For example, a UA is not
required to render color on a monochrome monitor.)
An authoring tool is conformant to this specification
if it writes style sheets that are syntactically correct according to the
generic CSS grammar and the individual grammars of each feature in
this module, and meet all other conformance requirements of style sheets
as described in this module.
So that authors can exploit the forward-compatible parsing rules to
assign fallback values, CSS renderers must
treat as invalid (and ignore
as appropriate) any at-rules, properties, property values, keywords,
and other syntactic constructs for which they have no usable level of
support. In particular, user agents must not selectively
ignore unsupported component values and honor supported values in a single
multi-value property declaration: if any value is considered invalid
(as unsupported values must be), CSS requires that the entire declaration
be ignored. To avoid clashes with future CSS features, the CSS2.1 specification
reserves a prefixed
syntax for proprietary and experimental extensions to CSS.
Prior to a specification reaching the Candidate Recommendation stage
in the W3C process, all implementations of a CSS feature are considered
experimental. The CSS Working Group recommends that implementations
use a vendor-prefixed syntax for such features, including those in
W3C Working Drafts. This avoids incompatibilities with future changes
in the draft.
Once a specification reaches the Candidate Recommendation stage,
non-experimental implementations are possible, and implementors should
release an unprefixed implementation of any CR-level feature they
can demonstrate to be correctly implemented according to spec.
To establish and maintain the interoperability of CSS across
implementations, the CSS Working Group requests that non-experimental
CSS renderers submit an implementation report (and, if necessary, the
testcases used for that implementation report) to the W3C before
releasing an unprefixed implementation of any CSS features. Testcases
submitted to W3C are subject to review and correction by the CSS
Working Group.
Further information on submitting testcases and implementation reports
can be found from on the CSS Working Group’s website at
http://www.w3.org/Style/CSS/Test/.
Questions should be directed to the
public-css-testsuite@w3.org
mailing list.
Row
Column
Start
row-start line
column-start line
End
row-end line
column-end line
Span
row span
column span
Position
Span
Definite
At least one specified line
Explicit, implicit, or defaulted span.
Note: Non-subgrids default to 1.
Automatic
No lines explicitly specified
Subgrid without an explicit or implied span
8.1
Common Patterns for Grid Placement
grid-area
grid-column
grid-row
grid-column-start
grid-column-end
grid-row-start
grid-row-end
8.1.1
Named Areas
article {
grid-area: main;
/* Places item into the named area "main". */
}
.one {
grid-row-start: main;
/* Align the row-start edge to the start edge of the "main" named area. */
}
8.1.2
Numeric Indexes and Spans
.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;
}
.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;
/* Ends in the 7th row,
spans 5 rows up (starting in the 2nd row). */
}
8.1.3
Named Lines and Spans
.five {
grid-column: first / middle;
/* Span from line "first" to line "middle". */
}
.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. */
}
8.1.4
Auto Placement
.eight {
grid-area: auto; /* Initial value */
}
.nine {
grid-area: span 2 / span 3;
/* Auto-placed item, covering two rows and three columns. */
}
8.1.5
Auto Sizing Subgrids
.adverts {
grid: subgrid;
grid-column: span 2;
}
.adverts
block,
if no further rules specified its position.
8.2
Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
Name: grid-row-start, grid-column-start, grid-row-end, grid-column-end Value: "><grid-line> Initial: auto Applies to: grid items Inherited: no Media: visual Computed value: specified value Percentages: n/a "><grid-line> =
auto |
"><custom-ident> |
[ "><integer> && "><custom-ident>? ] |
[ span && [ "><integer> || "><custom-ident> ] ]
1 2 3 4 5 6 7 8 9
+--+--+--+--+--+--+--+--+
| | | | | | | | |
A B C A B C A B C
| | | | | | | | |
+--+--+--+--+--+--+--+--+
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 */
8.2.1
Grid Placement Error Handling
8.3
Placement Shorthands: the grid-column, grid-row, and grid-area properties
Name: grid-row, grid-column Value: "><grid-line> [ / "><grid-line> ]? Initial: see individual properties Applies to: grid items Inherited: see individual properties Media: visual Computed value: see individual properties Percentages: see individual properties Name: grid-area Value: "><grid-line> [ / "><grid-line> ]{0,3} Initial: see individual properties Applies to: grid items Inherited: see individual properties Media: visual Computed value: see individual properties Percentages: see individual properties 8.4
Absolutely-positioned Grid Items
.grid {
grid: 10rem 10rem 10rem 10rem / 1fr 1fr 1fr 1fr;
/* 4 columns of 10rem each,
4 equal-height rows filling the grid container */
justify-content: center;
/* center the grid horizontally within the grid container */
position: relative;
/* Establish abspos containing block */
}
.abspos {
grid-row-start: 1; /* 1st grid row line = top of grid container */
grid-row-end: span 2; /* 3rd grid row line */
grid-column-start: 3; /* 3rd grid col line */
grid-column-end: auto; /* right padding edge */
/* Containing block covers the top right quadrant of the grid container */
position: absolute;
top: 70px;
bottom: 40px;
left: 100px;
right: 30px;
}
9
Alignment
9.1
Aligning with auto margins
9.2
Row-axis Alignment: the justify-self and justify-items properties
9.3
Column-axis Alignment: the align-self and align-items properties
9.4
Aligning the Grid: the justify-content and align-content properties
.grid {
display: grid;
grid: 12rem 12rem 12rem 12rem / 10rem 10rem 10rem 10rem;
justify-content: end;
align-content: center;
}
9.5
Z-axis Ordering: the z-index property
<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: foot }
#B { grid-column: 1; grid-row: 1; z-index: 10 }
#C { grid-column: 2; grid-row: 1; align-self: head; margin-left: -20px }
#D { grid-column: 2; grid-row: 2; justify-self: end; align-self: head }
#E { grid-column: 1 / span 2; grid-row: 1 / span 2;
z-index: 5; justify-self: center; align-self: center; }
</style>
<div id="grid">
<div id="A">A</div>
<div id="B">B</div>
<div id="C">C</div>
<div id="D">D</div>
<div id="E">E</div>
</div>
9.6
Grid Baselines
10
Track Sizing Algorithm
10.1
Definition of Terms for use in Calculating Grid Track Sizes
10.2
Overall Sizing Algorithm
10.3
Initialize Track Sizes
10.4
Resolve Content-Based Track Sizing Functions
extra-space = max(0, size-contribution - ∑track-sizes)
10.5
Grow All Tracks To Their Max
10.6
Process Flexible Tracks
10.6.1
Find the Size of an fr
11
Fragmenting Grid Layout
Acknowledgements
Changes
Appendix A: Translated Layout Algorithm
Conformance
Document conventions
class="example"
,
like this:
class="note"
, like this:
Conformance classes
Partial implementations
Experimental implementations
Non-experimental implementations
References
Normative References
Informative References
Index
Property index
Name Value Initial Applies to Inh. %ages Media Computed value grid-template-columns none | <track-list> | subgrid <line-name-list>? none grid containers no refer to corresponding dimension of the content area visual As specified, except for auto (see prose), with lengths made absolute
grid-template-rows none | <track-list> | subgrid <line-name-list>? none grid containers no refer to corresponding dimension of the content area visual As specified, except for auto (see prose), with lengths made absolute
grid-template-areas none | <string>+ none grid containers no n/a visual specified value
grid-template none | subgrid | <‘grid-template-columns’> / <‘grid-template-rows’> | [ <track-list> / ]? [ <line-names>? <string> <track-size>? <line-names>? ]+ see individual properties grid containers see individual properties see individual properties visual see individual properties
grid-auto-columns <track-size> auto grid containers no see Track Sizing visual see Track Sizing
grid-auto-rows <track-size> auto grid containers no see Track Sizing visual see Track Sizing
grid-auto-flow [ row | column ] && dense? | stack && [ row | column ]? row grid containers no n/a visual specified value
grid <‘grid-template’> | [ <‘grid-auto-flow’> [ <‘grid-auto-columns’> [ / <‘grid-auto-rows’> ]? ]? ] see individual properties grid containers see individual properties see individual properties visual see individual properties
grid-row-start <grid-line> auto grid items no n/a visual specified value
grid-column-start <grid-line> auto grid items no n/a visual specified value
grid-row-end <grid-line> auto grid items no n/a visual specified value
grid-column-end <grid-line> auto grid items no n/a visual specified value
grid-row <grid-line> [ / <grid-line> ]? see individual properties grid items see individual properties see individual properties visual see individual properties
grid-column <grid-line> [ / <grid-line> ]? see individual properties grid items see individual properties see individual properties visual see individual properties
grid-area <grid-line> [ / <grid-line> ]{0,3} see individual properties grid items see individual properties see individual properties visual see individual properties Issues Index