Building Production-Ready CSS Grid Layouts Today
- Grid container -- element containing a grid, defined by setting
display: grid;. - Grid item -- any direct descendant of a grid container.
- Grid line -- horizontal (row) or vertical (column) line separating the grid into sections. Numerated from
1rather than0. - Grid cell -- a cell inside the grid (intersection of single lines).
- Grid area -- a rectangular area between four specified grid lines; can cover one or more cells.
- Grid track -- the space between two or more adjacent grid lines.
- Grid gap -- the space between grid tracks (aka "gutters").
<div class="site">
</div>.site {
display: grid;
}.site {
display: grid;
grid-template-columns: 2fr 1fr 1fr;
grid-template-rows: auto 1fr 3fr;
}The grid items automatically populate frid from top left to bottom right based on HTML source order.
<div class="site">
<h1 class="header">My Header</h1>
<h1 class="main-content">Main Content</h1>
<h1 class="side-content">Side Content</h1>
</div>
.header {
grid-column: 1/4;
grid-row: 1/2;
}
.main-content {
grid-column: 1/3;
grid-row: 2/4;
}
.side-content {
grid-column: 3/4;
grid-row: 2/4;
}.site {
display: grid;
grid-template-columns: 2fr 1fr 1fr;
grid-template-rows: auto 1fr 3fr;
grid-template-areas:
"header header header"
"main main side"
"main main side";
}.header {
grid-area: header;
}
.main-content {
grid-area: main;
}
.side-content {
grid-area: side;
}Use @support feature query.
@supports (display: grid) { ... }...or better
@supports (grid-area: auto) { ... }- Build accessible mobile-first layout without grid.
- Use mobile-first layout as fallback for all browsers.
- Use
@supportto detect grid support. - At the appropriate breakpoint, create layout with grid and grid-areas.
- Explore new layouts as viewport widens.