← Back to HTML & CSS, page 9

html-css-0180 · Hard senior

Build a header, a left menu, main content, a right sidebar and a footer that stays at the bottom. How do you keep the HTML order sensible?

Write the HTML in a sensible reading order, make body a grid with named areas, columns of fixed side widths and a fluid centre, and rows auto, 1fr and auto so the footer stays at the bottom. Stack the areas in a media query for small screens.

Full answer

The holy grail layout has a header, three columns (menu, main content and side content) and a footer. The old challenge was equal height columns, a fluid centre and content that comes first in the HTML, using floats. With Grid, it is short.

Steps:

  1. Write the HTML in a reading order that makes sense: a skip link, header, nav, main, aside, footer. Keyboard and screen reader users follow this order, so the order should be the one a person would expect.
  2. Make body a grid with min-height: 100vh (and 100dvh after it). Name the areas: header across the top, nav, main and aside in the middle, footer across the bottom.
  3. Use grid-template-columns: 160px 1fr 160px so the centre is fluid, and grid-template-rows: auto 1fr auto. The 1fr row takes the free space, so the footer is pushed to the bottom of the window on short pages and moves down on long pages. The columns stretch, so they have equal height with no extra tricks.
  4. Make it responsive. Below a breakpoint, use one column with areas in the order of the HTML. On narrow screens, the menu may become a button menu.
  5. Add min-width: 0 or use minmax(0, 1fr) for the centre, so long content cannot widen the grid.

If the content must come before the menu in the HTML, for example for a long navigation, use a skip link, and keep the visual order close to the source order. Large differences between visual and source order harm keyboard users.

An older version of the same layout uses Flexbox: a column flex body with flex: 1 on a middle row that is itself a row flex container. Both work. Grid is clearer here because the layout is two-dimensional.

On a wide window, the menu is on the left, the main content is in the middle with the rest of the width, and the sidebar is on the right. The footer touches the bottom of the window even with little content. Below 600 pixels, everything stacks in the HTML order.

Test with long content, and with the keyboard, to confirm that the focus order matches what you see.

html
<style>
  body { margin: 0; min-height: 100vh; min-height: 100dvh; display: grid; font-family: sans-serif;
    grid-template-columns: 160px minmax(0, 1fr) 160px;
    grid-template-rows: auto 1fr auto;
    grid-template-areas: "h h h" "n m a" "f f f"; }
  header { grid-area: h; background: #cde; }
  nav { grid-area: n; background: #fdc; }
  main { grid-area: m; }
  aside { grid-area: a; background: #dfc; }
  footer { grid-area: f; background: #333; color: #fff; }
  .skip { position: absolute; left: 8px; top: -3em; }
  .skip:focus { top: 8px; }
  @media (max-width: 600px) {
    body { grid-template-columns: minmax(0, 1fr); grid-template-areas: "h" "n" "m" "a" "f"; grid-template-rows: auto; }
  }
</style>
<a href="#main" class="skip" id="skip">Skip to content</a>
<header id="h">Header</header>
<nav id="n">Menu</nav>
<main id="main"><p>Short content</p></main>
<aside id="a">Side</aside>
<footer id="f">Footer</footer>

Checked at a width of 800 pixels and at 400 pixels in Chrome. The focus order was not tested with a screen reader. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • HTML order should match the reading and focus order.
  • grid-template-areas names header, nav, main, aside and footer.
  • Rows auto, 1fr and auto give a sticky footer.
  • Columns stretch, so they have equal height.
  • Stack areas on small screens.

Common mistakes

  • Using floats and clearfix hacks for this layout today.
  • Placing content first in the HTML and the menu first on screen with no skip link.
  • Forgetting min-height or the 1fr row, so the footer floats in the middle of the screen.

Follow-ups

  • Why does the centre column use minmax(0, 1fr)?
  • How does the layout change on a phone?

Tips

  • Say that the HTML reading order comes first, and the grid areas follow.
  • Explain how rows auto, 1fr and auto give the sticky footer.