Title: CSS Grid Layout Module Level 2 Shortname: css-grid Level: 2 Status: ED Group: csswg Work Status: exploring TR: https://www.w3.org/TR/css-grid-2/ ED: https://drafts.csswg.org/css-grid-2/ Previous version: https://www.w3.org/TR/2018/WD-css-grid-2-20180206/ Editor: Tab Atkins Jr., Google, http://www.xanthir.com/contact/, w3cid 42199 Editor: Elika J. Etemad / fantasai, Invited Expert, http://fantasai.inkedblade.net/contact, w3cid 35400 Editor: Rossen Atanassov, Microsoft, ratan@microsoft.com, w3cid 49885 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. Level 2 expands Grid by adding “subgrid” capabilities for nested grids to participate in the sizing of their parent grids; and aspect-ratio–controlled gutters.Introduction {#intro} ===================== This level is currently maintained as a diff spec over the level 1 module [[!CSS-GRID-1]]. The main addition to Level 1 is the “subgrid” feature: a subgridded axis is one which matches up its grid lines to lines in the element's parent's grid, and which derives the sizes of its tracks through this integration with the parent grid. In general, the behavior of subgrids are the same in both proposals: it is only a question of whether a single-axis subgrid is possible. Issue: The full text of the Grid specification will be folded in when this draft reaches CR.
<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 / auto 1fr; } li { grid-column: span 2; display: grid; grid-template-columns: subgrid; border: solid; } label { grid-column: 1; } input { grid-column: 2; }
Name: grid-template-rows, grid-template-columns New values: subgrid <>?
<line-name-list> = [ <The <> | < > ]+ <line-names> = '[' < >* ']' <name-repeat> = repeat( [ | auto-fill ], +)
In this process, the sum of the subgrid's margin, padding, and borders at each edge are applied as an extra layer of (potentially negative) margin to the items at those edges. This extra layer of “margin” accumulates through multiple levels of subgrids.
#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 size of those margins/borders/padding also influences sizing. For example, if the subgrid has 100px padding:
#subgrid { padding: 100px; }Then a grid item in the subgrid's first track acts as if it has an additional ''100px'' of top, left, and bottom margin, influencing the sizing of the parent grid's tracks and the grid item's own position.
Meanwhile, half the size of the difference between the subgrid’s gutters ('row-gap'/'column-gap') and its parent grid’s gutters is applied as an extra layer of (potentially negative) margin to the items not at those edges. This extra layer of “margin” also accumulates through multiple levels of subgrids. A value of ''row-gap/normal'' indicates that the subgrid has the same size gutters as its parent grid, i.e. the applied difference is zero. Note: The end result will be that the parent’s grid tracks will be sized as specified, and that the subgrid’s gaps will visually center-align with the parent grid’s gaps.
For example, suppose we have a 300px-wide outer grid with 50px gaps and its columns specified as ''100px 1fr''. A subgrid spanning both tracks would have…
Issue: Note, this means that a subgrid establishing an orthogonal flow would have the order of its track sizing inverted compared to a nested grid. We could simplify this by saying that an orthogonal flow cannot establish a subgrid; it can only be a nested grid.Once the size of each grid area is thus established, the grid items are laid out into their respective containing blocks. The grid area’s width and height are considered definite for this purpose. Note: Since formulas calculated using only definite sizes, such as the stretch fit formula, are also definite, the size of a grid item which is stretched is also considered definite.
- First, the track sizing algorithm is used to resolve the sizes of the grid columns. In this process, any grid item which is subgridded in the grid container’s inline axis is treated as empty and its grid items (the grandchildren) are treated as direct children of the grid container (their grandparent). This introspection is recursive. Items which are subgridded only in the block axis, and whose grid container size in the inline axis depends on the size of its contents are also introspected: since the size of the item in this dimension can be dependent on the sizing of its subgridded tracks in the other, the size contribution of any such item to this grid’s column sizing (see Resolve Intrinsic Track Sizes) is taken under the provision of having determined its track sizing only up to the same point in the Grid Sizing Algorithm as this parent grid itself. E.g. for the first pass through this step, the item will have its tracks sized only through this first step; if a second pass of this step is triggered then the item will have completed a first pass through steps 1-3 as well as the second pass of this step prior to returning its size for consideration in this grid’s column sizing. Again, this introspection is recursive. If calculating the layout of a grid item in this step depends on the available space in the block axis, assume the available space that it would have if any row with a definite max track sizing function had that size and all other rows were infinite.
- Next, the track sizing algorithm resolves the sizes of the grid rows, using the grid column sizes calculated in the previous step. In this process, any grid item which is subgridded in the grid container’s block axis is treated as empty and its grid items (the grandchildren) are treated as direct children of the grid container (their grandparent). This introspection is recursive. As with sizing columns, items which are subgridded only in the inline axis, and whose grid container size in the block axis depends on the size of its contents are also introspected. (As with sizing columns, the size contribution to this grid’s row sizing is taken under the provision of having determined its track sizing only up to this corresponding point in the algorithm; and again, this introspection is recursive.)
- Then, if the min-content contribution of any grid items have changed based on the row sizes calculated in step 2, steps 1 and 2 are repeated with the new min-content contribution and max-content contribution (once only).
This cycle is necessary for cases where the inline size of a grid item depends on the block size of its grid area. Examples include wrapped column flex containers (''flex-flow: column wrap''), orthogonal flows ('writing-mode'), and multi-column containers.- Finally, the grid container is sized using the resulting size of the grid as its content size, and the tracks are aligned within the grid container according to the 'align-content' and 'justify-content' properties. Note: This can introduce extra space between tracks, potentially enlarging the grid area of any grid items spanning the gaps beyond the space allotted to during track sizing.
Name: align-content, justify-content New values: [ <> < >? ]
.grid { grid-template-columns: repeat(auto-fill, 15em); gap: 1em; justify-content: space-evenly; align-content: 1ar space-between; }This next example is the same as the previous, except that the ratio between row and column gaps is φ:
.grid { grid-template-columns: repeat(auto-fill, 15em); gap: 1em 1.618em; justify-content: space-evenly; align-content: 0.618ar space-between; }