1. Introduction
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.
The full text of the Grid specification will be folded in when this draft reaches CR.
2. Subgrids
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 and columns to its parent grid container, making it a subgrid. 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 / auto 1fr; } li { grid-column: span 2; display: grid; grid: subgrid; border: solid; } label { grid-column: 1; } input { grid-column: 2; }
2.1. Establishing a Subgrid: Per-Axis Proposal
Subgrids provide the ability to pass grid parameters down through nested elements, and content-based sizing information back up to their parent grid.
Name: | grid-template-rows, grid-template-columns |
---|---|
New values: | subgrid |
- subgrid
- Tracks are subgridded in this axis: the tracks are adopted from the the parent grid tracks that it spans. If there is no parent grid, equivalent to the initial value, none.
Add back syntax for overlaying line names. (See old draft defining ''subgrid <line-name-list>?''.)
2.2. Characteristics of a Subgrid Item
A subgrid behaves just like a normal grid container except that:
- The number of explicit tracks in each subgridded dimension is given by its grid span in that dimension, rather than by grid-template-rows/grid-template-columns.
- The grid-placement properties of the subgrid’s grid items are scoped to the lines covered by the subgrid. E.g., numeric indices count starting from the first line of the subgrid rather than the first line of the parent grid.
- Since subgrids can be placed before their contents are placed, the subgridded lines automatically receive the line names specified on the parent grid.
-
The subgrid itself lays out as an ordinary grid item in its parent grid,
but acts as if it was completely empty for sizing purposes
in the subgridded dimension.
The subgrid’s own grid items participate in the sizing of its parent grid in the subgridded dimension(s) and are aligned to it in those dimensions. In this process, the sum of the subgrid’s margin, padding, and borders at each edge are applied as an extra layer of margin to the items at those edges.
For example, if we have a 3×3 grid with the following tracks:#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.
-
The gap properties do not apply
in the subgridded dimension(s).
(Their items are instead spaced by the corresponding gutters of their parent grid,
since they are laid out in the parent grid.)
See issue 2285 for discussion about customizing gaps within the subgrid.
-
The subgrid does not have any implicit grid tracks in the subgridded dimension(s);
every grid area within it is clamped to its explicit grid.
For example, if a span 1 subgrid has a grid item with grid-column: 2 / span 3;, then that item is instead forced into (and limited to) the first (only) track in the subgrid.
- The subgrid is always stretched in its subgridded dimension(s: the align-self/justify-self properties on it are ignored, as are any specified width/height constraints.
- Layoutwise, the subgrid’s grid is always aligned with the corresponding section of the parent grid; the align-content/justify-content properties on it are also ignored in the subgridded dimension.
- The overflow property does apply to subgrids, so that overflowing contents of the subgrid can be scrolled into view. (Note: the act of scrolling does not affect layout.)
2.3. Subgrid Sizing Algorithm
Note: Placement of all grid items, including subgrids and their sub-items, occurs before sizing.
Track sizing in a subgridded dimension treats each item in a given track in that axis as members of the parent grid. This interlacing requires that grid sizing drills down per axis into subgrids, rather than completing both axes in its recursion. Thus the Grid Sizing Algorithm is modified as follows:
- 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 elements.- 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.
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.
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.
Suppose we have a parent grid container A which contains an item B that has subgridded columns and contains a grandchild B that has subgridded rows and grandchild D that is simply a nested grid.
<grid-A> <grid-B subgrid=columns> <grid-C subgrid=rows/> <grid-D> </grid-B> <grid-A>
When A sizes its columns it treats B’s items as slotted into to A’s corresponding columns, but when A sizes its rows it treates B as a single item (a grid container with its own rows and some items including items C and D). Similarly when B sizes its rows, it treats C’s items as slotted into B’s rows, but when B sizes its columns, it treats C as a single item, just as it does with D. There is no relationship between C’s rows and A’s rows, because the rows in B are nested, not subgridded.
At a high level, the grid algorithm is:
- Size the columns
- Size the rows
- Adjust the columns (if needed based on final row sizes)
The grid sizing algorithm in this example would thus look like this:
-
Resolve sizes of A’s grid columns,
using the sizes of A’s grid items,
treating B as empty
but treating its children
(including C and D)
as items in grid A.
The grid algorithm simply recurses into D. For C, it’s more complicated:
- Size C’s columns.
- Size C’s rows by sizing B’s rows.
- Adjust C’s columns.
- Return C’s final column sizes.
A correct size for B’s rows requires C’s final column sizes, because the row size depends on the column size, and thus B’s rows could very well depend on C’s final column sizes. To break this cyclic dependency, we need to split the algorithm to depend on the initial approximation of C’s final column sizes, and do the adjustment pass later. So for C, we need to recurse into column sizing only, and pass that initial size up to A for its initial column sizing.
When we size B’s rows later on, we will size C’s rows (which are subgridded), and finish up C’s sizing by finalizing its columns. If this resulted in a change, we have the opportunity to trigger an adjustment pass for A’s columns during its adjustment pass.
-
Next, resolve sizes of A’s rows,
using the sizes of A’s grid items,
treating B as a single item.
Since B, as a subgrid, has its sizing is split out into the multiple passes, the grid algorithm issues only a row-sizing recursion into B: Size B’s rows, treating D as a single item, requesting its final size, and treating C as an empty item and hoisting its children as items into grid B.
B returns its final row size, which factors into A’s row sizing pass.
- Last, finalize A’s column sizes. If C’s final size changes as a result of the row-sizing pass through B, this should trigger a resizing of B’s columns, which should trigger a resizing pass on A’s column.
3. Aspect-ratio–controlled Gutters
There’s a desire for having row and column gaps maintain a particular aspect ratio. This is one proposal for doing so; other ideas are welcome. See discussion in Issue 1116. Note this feature is likely to move to css-align-4, it is just being drafted up here while css-align-3 stabilizes.
Name: | align-content, justify-content |
---|---|
New values: | [ <aspect-ratio> <content-distribution>? ] |
- <aspect-ratio>
-
A dimension with the unit ar,
representing a multiplier against the analogous quantity
in the other dimension.
If that quantity cannot be determined
(e.g. is itself specified as a <aspect-ratio>, or otherwise can’t be referenced),
then it is assumed to be zero.
Note: This value can expand gutters even when there is no free space left, causing overflow.
Specifically, an align-content value of 1ar represents the amount of space (which may be zero) allocated between two adjacent alignment subjects (grid tracks / flex lines / column boxes) by the justify-content property. Unless a different <content-distribution> value is specified, space is distributed according to the same <content-distribution> rules as for justify-content. The behavior of <number> values for justify-content is analogous.
Note: The space allocated by align-content: 1ar will be zero if justify-content does not allocate space between adjacent alignment subjects: either due to not having a <content-distribution> value or due to there being fewer than two alignment subjects.
If both align-content and justify-content have <number> values, then justify-content’s <number> value is ignored and its <content-distribution> value honored as if specified alone. If no <content-distribution> value was specified, then justify-content takes align-content’s <content-distribution> value (if one was specified) and otherwise falls back to space-between.
.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; }