Emmanuel Akinfulubi

HTML and CSSPractical II

Holy grail layout with CSS grid

24 The holy grail, in grid

hard

Build the classic five-part layout — header, left nav, main, right aside, footer — with a fixed sidebar, a fluid centre, and the footer always last. Mobile first: one column by default.

Stuck? Three hints, smallest first
  1. 1. Default to a single column, then add the three-column template inside a min-width query.
  2. 2. Line and area names both work. Areas keep the intent readable at a glance.
  3. 3. The centre column is 1fr; the two sides are fixed, so only the middle absorbs the change in width.

index.html

<div class="grail">
  <header class="g-head">Header</header>
  <nav class="g-nav" aria-label="Sections">Nav</nav>
  <main class="g-main">Main content, the only part that changes width.</main>
  <aside class="g-side">Aside</aside>
  <footer class="g-foot">Footer</footer>
</div>

style.css

/* One column is the default, so a narrow screen needs no query at all. */
.grail {
  display: grid;
  grid-template-areas:
    'head'
    'nav'
    'main'
    'side'
    'foot';
  gap: 8px;
  min-height: 200px;
}

@media (min-width: 34em) {
  .grail {
    grid-template-areas:
      'head head head'
      'nav  main side'
      'foot foot foot';
    grid-template-columns: 80px 1fr 80px;
    grid-template-rows: auto 1fr auto;
  }
}

.g-head { grid-area: head; }
.g-nav  { grid-area: nav; }
.g-main { grid-area: main; }
.g-side { grid-area: side; }
.g-foot { grid-area: foot; }

.grail > * {
  padding: 10px;
  border-radius: 6px;
  border: 1px solid #3055d8;
  background: #eef1fd;
  font-size: 0.8rem;
}

.g-main { background: #ffffff; }
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 24

const wide = (/@media[^{]*\{([\s\S]*?)\n  \}/.exec(css) || [, ''])[1];
const wideRows = [...wide.matchAll(/'([a-z. ]+)'/g)].map((m) => m[1].trim().split(/\s+/));

test('the default is a single column', /\.grail\s*\{[\s\S]*?'head'[\s\S]*?'foot'/.test(css), true);
test('the three-column version lives in a min-width query', /@media \(min-width: 34em\)/.test(css), true);
test('which is mobile first, not max-width', /max-width/.test(css), false);
test('the wide template has three rows of three cells', wideRows.map((r) => r.length), [3, 3, 3]);
test('the middle row is nav, main, aside', wideRows[1], ['nav', 'main', 'side']);
test('only the centre column is fluid', /grid-template-columns:\s*80px 1fr 80px/.test(css), true);
test('all five areas are claimed', ['head', 'nav', 'main', 'side', 'foot'].every((a) => new RegExp('grid-area:\\s*' + a + ';').test(css)), true);
test('the footer is last in the markup as well as the layout', html.lastIndexOf('g-foot') > html.lastIndexOf('g-side'), true);

Output


      

This layout took a decade of float and table hacks and is now nine lines. Two things are deliberate. It is mobile first: one column is the default and the query adds the sidebars, which fails better than the reverse if the query never matches. And the markup order matches the reading order — grid can place areas anywhere, so it is entirely possible to build a layout that looks right and tabs in nonsense order. The last check is a cheap guard against exactly that.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.