Page layouts

Topic 1 of 4 in Layout patterns7 min readBeginnerFree course

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 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:

CSS
body {
  min-block-size: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;   /* header, main, footer */
}
  • min-block-size: 100dvh makes 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).
  • 1fr gives 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:

A classic page layout with grid: header across the top, nav, main and aside in the middle, footer across the bottom. Code: body { min-block-size: 100dvh; display: grid; grid-template: “header header header” auto “nav main aside” 1fr “footer footer footer” auto / 12rem 1fr 14rem; }. The 1fr middle row grows, so the footer stays at the bottom on short pages.

CSS
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 plain 1fr won’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 own overflow-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:
CSS
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:

CSS
.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:

CSS
.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-end and content-start / content-end creates implicit areas called full and content, so grid-column: content reads 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:

CSS
.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 auto rows for the shell, named areas inside a media query, minmax(0, 1fr) for main, and a content grid for readable articles.

Exercise

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.

HTML
<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>

Summary

  • A grid with grid-template-rows: auto 1fr auto and min-block-size: 100dvh keeps 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.