Page layouts
Overview
Every site starts with a page shell: a header, some navigation, the main content, perhaps a sidebar, and a footer. Building these used to require floats, clearfixes, negative margins and “faux columns” images. With grid, each classic layout is a handful of lines. This lesson collects the patterns you’ll use on almost every project.
The sticky footer
The problem: on a page with little content, the footer floats up into the middle of the screen. The fix is a three-row grid where the middle row takes the leftover space:
body {
min-block-size: 100dvh;
display: grid;
grid-template-rows: auto 1fr auto; /* header, main, footer */
}min-block-size: 100dvhmakes the body at least as tall as the window (using dynamic viewport units so mobile browser bars don’t cause a jump or a scroll).1frgives the main row all the leftover height, pushing the footer to the bottom.- When the content is taller than the window, the body simply grows and the footer follows the content. Nothing is fixed or positioned.
The holy grail
The “holy grail” is the classic header, three columns and footer:
body {
min-block-size: 100dvh;
display: grid;
grid-template:
"header header header" auto
"nav main aside" 1fr
"footer footer footer" auto
/ 12rem minmax(0, 1fr) 14rem;
}
header { grid-area: header; }
nav { grid-area: nav; }
main { grid-area: main; }
aside { grid-area: aside; }
footer { grid-area: footer; }Two details matter:
minmax(0, 1fr)for the main column. As you saw in Tracks and the fr unit, a plain1frwon’t shrink below the width of a long code block or a wide table inside it, and the page scrolls sideways.minmax(0, 1fr)lets the column shrink and keeps the overflow inside (where you can give the code block its ownoverflow-x: auto).- Mobile first. On small screens, don’t define areas at all — the elements stack in source order. Add the template inside a media query:
body { min-block-size: 100dvh; display: grid; grid-template-rows: auto auto 1fr auto auto; }
@media (width >= 60rem) {
body { grid-template: /* …areas as above… */; }
}The five-row template on small screens makes main (the third child) the row that grows, so the footer still sits at the bottom.
Centred content with a max width
Most pages have a centred content area with gutters on small screens:
.container {
inline-size: min(70rem, 100% - 2rem);
margin-inline: auto;
}min(70rem, 100% - 2rem) means “70rem, but always leave 1rem on each side”. One line replaces a max-width + padding combination.
The content grid: reading column with breakouts
Articles and marketing pages often want a narrow column of text, with some elements — images, quotes, coloured bands — breaking out to the full width. Wrapping each section in its own container gets messy. A content grid solves it with three columns and named lines:
.content {
display: grid;
grid-template-columns:
[full-start] minmax(1rem, 1fr)
[content-start] min(65ch, 100% - 2rem) [content-end]
minmax(1rem, 1fr) [full-end];
}
.content > * { grid-column: content; } /* everything in the reading column */
.content > .full-bleed { grid-column: full; } /* except these */- The middle column is at most 65ch — a comfortable line length for reading.
- The outer columns take the remaining space equally (centring the middle one) and are at least 1rem wide (the gutters on small screens).
- Naming lines
full-start/full-endandcontent-start/content-endcreates implicit areas calledfullandcontent, sogrid-column: contentreads clearly.
You can add a third, wider “breakout” track between the two for images that should be wider than the text but not full width.
Dashboards and app shells
Apps often need a fixed-height screen where only the main area scrolls:
.app {
block-size: 100dvh;
display: grid;
grid-template: "sidebar header" auto "sidebar main" 1fr / 16rem minmax(0, 1fr);
}
.app main {
overflow: auto; /* only the content scrolls */
min-block-size: 0; /* allow the grid row to be smaller than its content */
}Notice block-size rather than min-block-size: the app fills the window exactly, and scrolling happens inside main.
Source order
In every example the HTML stays in a logical reading order — header, nav, main, aside, footer — and grid places the parts visually. Keyboard and screen-reader users experience the page in source order, so don’t reorder content with grid just to make the HTML shorter (more in Source order and reading order). Using landmark elements (<header>, <nav>, <main>, <aside>, <footer>) also lets screen-reader users jump between regions.
Starter kit:
min-block-size: 100dvh+auto 1fr autorows for the shell, named areas inside a media query,minmax(0, 1fr)for main, and a content grid for readable articles.
A complete page shell
Build the page shell: on small screens header, nav, main, aside and footer stack in that order. From 60rem, use named grid areas: header across the top, a 12rem nav, flexible main, a 16rem aside, and footer across the bottom. The footer must sit at the bottom of the window even when there’s little content. Inside main, make .content a centred column of readable text (max 65ch with 1rem gutters) where .full-bleed breaks out to the full width of main.
<body class="shell">
<header>Header</header>
<nav>Nav</nav>
<main>
<div class="content">
<p>Readable text in a centred column.</p>
<figure class="full-bleed">Full-width banner</figure>
<p>More text.</p>
</div>
</main>
<aside>Aside</aside>
<footer>Footer</footer>
</body>
<style>
* { box-sizing: border-box; }
body { margin: 0; }
header, nav, aside, footer { background: #eaf0fc; padding: 1rem; }
.full-bleed { background: #f1ecfd; margin: 0; padding: 2rem; }
/* your code here */
</style><body class="shell">
<header>Header</header>
<nav>Nav</nav>
<main>
<div class="content">
<p>Readable text in a centred column.</p>
<figure class="full-bleed">Full-width banner</figure>
<p>More text.</p>
</div>
</main>
<aside>Aside</aside>
<footer>Footer</footer>
</body>
<style>
* { box-sizing: border-box; }
body { margin: 0; }
header, nav, aside, footer { background: #eaf0fc; padding: 1rem; }
.full-bleed { background: #f1ecfd; margin: 0; padding: 2rem; }
.shell {
min-block-size: 100dvh;
display: grid;
grid-template-rows: auto auto 1fr auto auto; /* main grows on small screens */
}
@media (width >= 60rem) {
.shell {
grid-template:
"header header header" auto
"nav main aside" 1fr
"footer footer footer" auto
/ 12rem minmax(0, 1fr) 16rem;
}
header { grid-area: header; }
nav { grid-area: nav; }
main { grid-area: main; }
aside { grid-area: aside; }
footer { grid-area: footer; }
}
.content {
display: grid;
grid-template-columns:
[full-start] minmax(1rem, 1fr)
[content-start] min(65ch, 100% - 2rem) [content-end]
minmax(1rem, 1fr) [full-end];
}
.content > * { grid-column: content; }
.content > .full-bleed { grid-column: full; }
</style>Summary
- A grid with
grid-template-rows: auto 1fr autoandmin-block-size: 100dvhkeeps the footer at the bottom of short pages. - Named template areas describe the whole page in one readable block and are easy to rearrange at breakpoints.
- Use
minmax(0, 1fr)for the main column so wide content inside can’t blow out the layout. - A three-column ‘content grid’ with named lines gives a centred reading column plus full-bleed breakouts.
- Keep the source order logical — header, nav, main, aside, footer — and let grid handle placement.