subgrid

revision:


Content

introduction to subgrid subgrid on row axis and column axis ssubgrid for columns subgrid for rows subgrid in both dimensions (columns and rows) gap properties and subgrid named gridlines and subgrid conclusion


introduction to subgrid

top

When you add "display: grid" to a grid container, only the direct children become grid items and can then be placed on the grid that you have created. The children of these items display in normal flow.

You can "nest" grids by making a grid item a grid container. These grids however are independent of the parent grid and of each other, meaning that they do not take their track sizing from the parent grid. This makes it difficult to line nested grid items up with the main grid.

If you set the value "subgrid" on "grid-template-columns", "grid-template-rows" or both, instead of creating a new track listing the nested grid uses the tracks defined on the parent.

For example, if you use "grid-template-columns: subgrid" and the nested grid spans three column tracks of the parent, the nested grid will have three column tracks of the same size as the parent grid. Gaps are inherited but can also be overridden with a different "gap" value. Line names can be passed from the parent into the subgrid, and the subgrid can also declare its own line names.


subgrid on row axis and column axis

top

examples

outergrid
innergrid
outergrid_1
innergrid_1
code:
            <div class="grid_A">
                <div class="outergrid">outergrid
                    <div class="innergrid">innergrid</div>
                </div>
                <div class="outergrid_1">outergrid_1
                    <div class="innergrid_1">innergrid_1</div>
                </div>
            </div>
            <style>
                .outergrid {display: grid; width: 30vw;margin-left: 1vw; grid-template-rows: repeat(4, minmax(5vw, auto));  background-color: #abc; color: red;}
                .innergrid {display: grid;grid-row: 2 / 4; grid-template-rows: subgrid;background-color: green; color: skyblue;}
                .outergrid_1 {display: grid; width: 30vw; margin-left: 2vw; grid-template-columns: repeat(9, 1fr); background-color: #abc; color:red; }
                .innergrid_1 { display: grid; grid-column: 2 / 7; grid-template-columns: subgrid; grid-template-rows: repeat(3, 5vw); background-color: green; color:skyblue;}
            </style>
        


subgrid for columns

top

examples

grid
item
subitem

The "item" is made into a grid (columns 2/7, rows 2/4), giving it column tracks that are a subgrid and defining rows as normal. As the item spans five column tracks (2/7), the subgrid has five column tracks. The "subitem" is placed on this grid.

The rows are NOT a subgrid and so behave as a nested grid does normally. The grid area on the parent expands to be large enough for this nested grid.

"Line numbering" restarts inside the subgrid: column line 1, when inside the subgrid, is the first line of the subgrid. The subgridded element DOESN'T inherit the line numbers of the parent grid. This means that you can safely lay out a component that may be placed in different positions on the main grid, knowing that the line numbers on the component will always be the same.

code:
 
            <div class="grid_A">
                <div class="grid_1">grid
                    <div class="item">item
                        <div class="subitem">subitem</div>
                    </div>
                </div>
                <div class="text">
                   .......
                </div>
            </div>
            <style>
                .grid_1 {display: grid; width: 30vw;margin-left: 2vw; grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(4, minmax(5vw, auto)); background: rgb(44, 78, 44); border: 0.2vw solid red; color: red;}
                .item {display: grid; grid-column: 2 / 7; grid-row: 2 / 4; grid-template-columns: subgrid; grid-template-rows: repeat(3, 3vw); background: rgb(24, 202, 24); border: 0.2vw solid blue;color: skyblue;}
                .subitem {grid-column: 3 / 6; grid-row: 1 / 3; background: rgb(19, 243, 19); border: 0.2vw solid burlywood; color: burlywood;}
                .text{margin-left: -6vw;}    
            </style>
        


subgrid for rows

top

examples

grid_1
item_1
subitem_1

The setup is the same as the previous example, but "subgrid" is used as the value of "grid-template-rows", while defining explicit column tracks. The column tracks behave as a regular nested grid, but the rows are tied to the two tracks that the child spans.

code:
            <div class="grid_A">
                <div class="grid_2">grid_1
                    <div class="item_2">item_1
                    <div class="subitem_2">subitem_1</div>
                    </div>
                </div>
                <div class="text">
                   ...
                </div>
            </div>
            <style>
                .grid_2 {display: grid; width: 30vw;margin-left: 2vw; grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(4, minmax(5vw, auto)); background: rgb(44, 78, 44); border: 0.2vw solid red; color:red;}
                .item_2 {display: grid; grid-column: 2 / 7; grid-row: 2 / 4; grid-template-columns: repeat(3, 1fr); grid-template-rows: subgrid; background: rgb(24, 202, 24); border: 0.2vw solid blue; color:skyblue;}
                .subitem_2 {grid-column: 2 / 4; grid-row: 1 / 3; background: rgb(19, 243, 19); border: 0.2vw solid burlywood; color:burlywood;}
                .text1{margin-left: -6vw;} 
            </style>
        


subgrid in both dimensions (columns and rows)

top

examples

outergrid_2
innergrid_2
grid_2
item_2
subitem_2
code:
            <div class="grid_A">
                <div class="outergrid_2">outergrid_2
                    <div class="innergrid_2">innergrid_2</div>
                </div>
                <div class="grid_3">grid_2
                    <div class="item_3">item_2
                        <div class="subitem_3">subitem_2</div>
                    </div>
                </div>
                <style>
                    .outergrid_2 {display: grid; width: 30vw;margin-left: 2vw; grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(4, minmax(5vw, auto)); background-color: #abc; color:red;}
                    .innergrid_2 {display: grid; grid-column: 2 / 7; grid-row: 2 / 4; grid-template-columns: subgrid; grid-template-rows: subgrid; background-color: green; color:skyblue;}
                    .grid_3 {display: grid; width: 30vw;margin-left: 2vw; grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(4, minmax(5vw, auto)); background: rgb(44, 78, 44); border: 0.2vw solid red; color:red;}
                    .item_3 {display: grid;grid-column: 2 / 7; grid-row: 2 / 4;  grid-template-columns: subgrid; grid-template-rows: subgrid; background: rgb(24, 202, 24); border: 0.2vw solid blue; color:skyblue;}
                    .subitem_3 {grid-column: 3 / 6; grid-row: 1 / 3; background: rgb(19, 243, 19); border: 0.2vw solid burlywood; color:burlywood;}
                </style>
            </div>
        


gap properties and subgrid

top

examples

outergrid_3
innergrid_3
firstbox
firstbox2
grid
item
subitem
subitem2

A "gap", "column-gap", or "row-gap" specified on the parent will be passed into the subgrid, so that it will have the same spacing between tracks as the parent.

In some situations however you may wish the subgrid tracks to have a different gap or no gap. This can be achieved by using the gap-* properties on the grid container of the subgrid. In this example, the parent grid has a gap of 2vw for rows and columns. The subgrid has row-gap set to 0.

code:
            <div class="grid_A">
                <div class="outergrid_3">outergrid_3
                    <div class="innergrid_3">innergrid_3
                        <div class="firstbox">firstbox</div>
                        <div class="firstbox2">firstbox2</div>
                    </div>
                </div>
                <div class="gridB">grid
                    <div class="itemA">item
                        <div class="subitemA">subitem</div>
                        <div class="subitem2A">subitem2</div>
                    </div>
                </div>
            </div>
            <p>... </p>
            <p>....</p>
            <style>
                .outergrid_3 {display: grid; width: 30vw; margin-left: 2vw; grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(4, minmax(5vw, auto)); gap: 3vw; background-color: #abc; color:red;}
                .innergrid_3 {display: grid; grid-column: 2 / 7; grid-row: 2 / 4; grid-template-columns: subgrid; grid-template-rows: subgrid; row-gap: 1vw; background-color: green; color:skyblue;}
                .firstbox { grid-column: 3 / 6; grid-row: 1 / 3; background-color: #ffbada; color: lightgreen;}
                .firstbox2 {background-color: rgba(0,0,0,.5); grid-column: 2; grid-row: 1;  background-color: #abcdef; color: burlywood;}
                .gridB {display: grid; width: 30vw; margin-left: 2vw;grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(4, minmax(5vw, auto)); gap: 2vw; background: rgb(44, 78, 44); border: 0.2vw solid red;
                     color: red;}
                .itemA {display: grid; grid-column: 2 / 7; grid-row: 2 / 4;     grid-template-columns: subgrid; grid-template-rows: subgrid; row-gap: 0;background: rgb(16, 223, 16); border: 0.2vw solid darkgreen; 
                    color: skyblue;}
                .subitemA {grid-column: 3 / 6; grid-row: 1 / 3;background: rgb(245, 101, 101); border: 0.2vw solid dodgerblue; color:burlywood;}
                .subitem2A {grid-column: 2; grid-row: 1; background: rgb(142, 123, 226); border: 0.2vw solid skyblue; color: violet;}
            </style>
        


named gridlines and subgrid

top

examples

grid_1
item_1
subitem_1
grid_2
item_2
subitem_2
subitem2_2

When using CSS grid "lines on the grid" can be named and then items can be positioned based on those names rather than the line number. The line names on the parent grid are passed into the subgrid, and they can be used to place items. In the example, lines on the parent are named "col-start" and "col-end" and then they are used to place the subitem.

Line names can also be specified on the subgrid. This is achieved by adding a list of line names enclosed in square brackets after the "subgrid" keyword. If there are four lines in the subgrid, to name them all you could use the syntax "grid-template-columns: subgrid [line1] [line2] [line3] [line4]". Lines specified on the subgrid are added to any lines specified on the parent so you can use either or both. To demonstrate this, one item in the example is positioned using the parent lines, and one using the subgrid lines.

code:
            <div class="grid_A">
                <div class="grid_1A">grid_1
                    <div class="item_1A">item_1
                    <div class="subitem_1">subitem_1</div>
                    </div>
                </div>
                <div class="grid_2A">grid_2
                    <div class="item_2A">item_2
                        <div class="subitem_2A">subitem_2</div>
                        <div class="subitem2_2A">subitem2_2</div>
                    </div>
                </div>
            </div>
            <style>
                .grid_1A {display: grid; width: 30vw;margin-left: 2vw; grid-template-columns: 1fr 1fr 1fr [col-start] 1fr 1fr 1fr [col-end] 1fr 1fr 1fr; grid-template-rows: repeat(4, minmax(5vw, auto)); gap: 2vw;
                    background: rgb(44, 78, 44); border: 0.2vw solid red; color:red;}
                .item_1A {display: grid; grid-column: 2 / 7; grid-row: 2 / 4; grid-template-columns: subgrid; grid-template-rows: subgrid; background: rgb(16, 223, 16); border: 0.2vw solid darkgreen; color: skyblue;}
                .subitem_1A { grid-column: col-start / col-end; grid-row: 1 / 3; background: rgb(142, 123, 226); border: 0.2vw solid skyblue; color:burlywood;}
                .grid_2A {display: grid; width: 30vw;margin-left: 2vw;grid-template-columns: 1fr 1fr 1fr [col-start] 1fr 1fr 1fr [col-end] 1fr 1fr 1fr; grid-template-rows: repeat(4, minmax(5vw, auto)); gap: 2vw; 
                    background: rgb(44, 78, 44); border: 0.2vw solid red; color:red;}
                .item_2A {display: grid; grid-column: 2 / 7; grid-row: 2 / 4; grid-template-columns: subgrid [sub-a] [sub-b] [sub-c] [sub-d] [sub-e] [sub-f]; grid-template-rows: subgrid; background: rgb(16, 223, 16); 
                    border: 0.2vw solid darkgreen; color:skyblue }
                .subitem_2A {grid-column: col-start / col-end; grid-row: 1 / 3;background: rgb(51, 46, 71); border: 0.2vw solid skyblue; color: burlywood;}
                .subitem2_2A { grid-column: sub-b / sub-d; grid-row: 1;background: rgb(142, 123, 226); border: 0.2vw solid skyblue; color: green;}
            </style>
        


conclusion

top

Subgrids are an official substitute for nested grids to apply one grid inside another. It is scalable, responsive, and you don't need to adjust anything through extra code or Javascript. Being dependent on the parent container keeps it stuck to its assigned ratio and position.