CSS Grid Layout
Creates two-dimensional layouts with CSS Grid including template areas, responsive tracks, and auto-fit grids for complex page structures.
Code
/* Grid template areas */
.page-layout {
display: grid;
grid-template-areas:
"header header header"
"nav main aside"
"footer footer footer";
grid-template-columns: 200px 1fr 250px;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
gap: 1rem;
}
.page-layout > header { grid-area: header; }
.page-layout > nav { grid-area: nav; }
.page-layout > main { grid-area: main; }
.page-layout > aside { grid-area: aside; }
.page-layout > footer { grid-area: footer; }
/* Auto-fit responsive grid */
.auto-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1.5rem;
}
/* 12-column grid system */
.grid-12 {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 1rem;
}
.grid-12 .col-6 { grid-column: span 6; }
.grid-12 .col-4 { grid-column: span 4; }
.grid-12 .col-3 { grid-column: span 3; }
/* Masonry-like layout */
.masonry {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
grid-auto-rows: 10px;
gap: 1rem;
}
.masonry .item-tall { grid-row: span 30; }
.masonry .item-short { grid-row: span 20; }
/* Responsive grid with media query */
@media (max-width: 768px) {
.page-layout {
grid-template-areas:
"header"
"nav"
"main"
"aside"
"footer";
grid-template-columns: 1fr;
}
}How the snippet works
display: grid owns the layout: tracks are declared on the container, so children fall into place without wrapper markup.
max() resolves to the larger of the listed values, acting as a floor that still adapts.
gap spaces grid/flex items without margins on the children.
grid-template-columns declares the column tracks the children flow into.
1 media query adjusts the layout where the current arrangement stops fitting.
The rules target /* Grid template areas */ .page-layout, .page-layout > nav, .page-layout > main, .page-layout > aside, .page-layout > footer — each selector names the element it restyles, in source order.
Interactive Tools for This Topic
FAQ
- Which elements does this stylesheet target?
- The rules bind to `/* Grid template areas */ .page-layout`, `.page-layout > nav`, `.page-layout > main`, `.page-layout > aside`; when two rules tie in specificity, the one written later in the file wins.
Report a problem with this exampleOpens a pre-filled issue on GitHub.