grid 1)is a CSS layout system that provides a mechanism:
With grid, you can easily defines layout that requires horizontal and vertical alignment such as the below form2)
A grid container 4) is the container element that has the display value set to:
#grid {
display: grid;
}
It contains the grid.
The grid is defined 7) with the following three properties:
They define the grid of a grid container by specifying its explicit grid tracks.
The grid property 8) is a shorthand that sets all of three explicit grid properties
Example: the following grid declares a grid with :
main {
grid: "H H "
"A B "
"F F " 30px
/ auto 1fr;
}
A grid area 9) is a logical space name:
The grid area are defined using:
Example:
#grid {
display: grid;
grid-template-areas: ". area1"
"area2 area1"
". area1";
}
main {
grid: auto 1fr auto / repeat(5, 1fr);
min-height: 100vh;
}
You can specify the position where the element should go, using :
Longhand properties | shorthand equivalent | full shorthand equivalent |
---|---|---|
grid-row-start grid-row-end 11) | grid-row 12) | grid-area 13) |
grid-column-start grid-column-end 14) | grid-column 15) | grid-area 16) |
Example:
Placement | grid-area | grid-row, grid-column | grid-row-start/end, grid-column-start/end |
---|---|---|---|
Place into named grid area a |
|
|
|
Auto-place into next empty area |
|
| |
Place into row 2, column 4 |
|
| |
Place into column 3, span all rows |
|
|
|
Place using named lines |
|
|
In css, you would apply them in rule block. Example in a 2 columns x 3 rows.
#stats { grid-column: 1; grid-row: 2; align-self: start; }
#controls { grid-column: 2; grid-row: 3; justify-self: center; }
Sizing a grid means sizing the tracks. See the tracks definition section to know how to size a grid.
The grid item 19) are:
As of Grid level 1, only the direct children of the container become grid items and can then be placed on the grid
A grid cell 20) is the intersection of:
It is the smallest unit of the grid that can be referenced when positioning grid items.
Grid track 21) is a generic term for:
It's the space between any two adjacent lines on the grid.
The below properties define grid tracks:
They controls how wide or tall the column or row may grow.
The sizing calculation is done by the grid sizing algorithm
Example of a Grid with two columns and three rows. 22)
#grid {
display: grid;
grid-template-columns: 150px 1fr; /* two columns */
grid-template-rows: 50px 1fr auto; /* three rows */
}
where:
grid lines can be referred to by:
The following code 23) names the lines in the grid with the sizing properties. (Some of the lines have multiple names)
#grid {
display: grid;
grid-template-columns: [first nav-start] 150px [main-start] 1fr [last];
grid-template-rows: [first header-start] 50px [main-start] 1fr [footer-start] 50px [last];
}
The sized grid is aligned 24) 25) according to the grid container’s:
Example:
.grid {
display: grid;
grid: 12rem 12rem 12rem 12rem / 10rem 10rem 10rem 10rem;
justify-content: end;
align-content: center;
}
main {
grid: auto-flow 1fr / repeat(auto-fill, 5em);
min-height: 100vh;
justify-content: space-between;
align-content: safe center;
}
The content may overflow if the cells in a grid are Block|.
One solution is to set the cell to a grid display with an autoflow to row.
display: grid;
grid-auto-flow: row;
By default, a grid item has a span of 1. 26)
Different spans can be given explicitly:
.three {
grid-row: 2 / span 5;
}
.four {
grid-row: span 5 / 7;
}
.five {
grid-column: first / middle;
}
.six {
grid-row: text 5 / text 7;
}
This example 27) shows how it's possible to change the layout with different media query.
@media (orientation: portrait) {
#grid {
display: grid;
grid-template-areas: "title stats"
"score stats"
"board board"
"ctrls ctrls";
...
}
}
@media (orientation: landscape) {
#grid {
display: grid;
/* Different positions in the layout */
grid-template-areas: "title board"
"stats board"
"score ctrls";
...
}
}
floating is ignored
See How to solve a CSS grid overflow ?
The browser devtool have option to visualize the grid.
For instance, on chrome: