Skip to main content

Design and frontend · Beginner · 18 min

How to Build Page Layouts with CSS Grid

By Padding · Updated 15 Nov 2026
Modern browsers · CSS Grid Layout Level 1 · checked in Chromium 159

You will build a page with a header, sidebar, main area and footer using named grid areas, fill it with a grid of cards that adds and removes columns by itself, make one card span two columns, and change the layout for narrow screens.

On this page

CSS Grid is the layout system built into browsers for putting things in rows and columns at the same time. Before it, page layouts used floats and tables, and later flexbox, which arranges items along one line. Grid works in two dimensions, so a header that spans the width, a sidebar next to the main content and a footer under both are a few lines of CSS.

You will build one small page and change it step by step. It has the page-level layout (header, sidebar, main, footer), a grid of cards in the main area, and a different arrangement on phones. Every property comes from the CSS Grid Layout specification, and current versions of Chrome, Edge, Firefox and Safari support it all.

Step 1 · Create the page

Make a folder with an HTML file and an empty stylesheet. The page has four regions, written as semantic elements, and six cards in the main region:

Bash
mkdir -p ~/grid-demo
cd ~/grid-demo
cat > index.html <<'HTML'
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Grid demo</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="page">
    <header class="site-header">Grid demo</header>
    <nav class="sidebar" aria-label="Sections">
      <a href="#one">Overview</a>
      <a href="#two">Guides</a>
      <a href="#three">About</a>
    </nav>
    <main class="content">
      <h1>Guides</h1>
      <section class="cards">
        <article class="card featured"><h2>Getting started</h2><p>A short tour.</p></article>
        <article class="card"><h2>Install</h2><p>Set it up.</p></article>
        <article class="card"><h2>Configure</h2><p>Make it yours.</p></article>
        <article class="card"><h2>Deploy</h2><p>Put it online.</p></article>
        <article class="card"><h2>Monitor</h2><p>Keep watch.</p></article>
        <article class="card"><h2>Upgrade</h2><p>Stay current.</p></article>
      </section>
    </main>
    <footer class="site-footer">Footer</footer>
  </div>
</body>
</html>
HTML

The viewport meta tag tells phones to use their real width. Without it, a phone pretends to be about 980 pixels wide and shrinks the page, and your narrow-screen rules never apply.

Add basic styles now, with no layout yet: a font, spacing and visible borders so you can see each box.

Bash
cat > style.css <<'CSS'
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; line-height: 1.5; }
.site-header, .sidebar, .content, .site-footer { padding: 1rem; }
.site-header { background: #1f3a5f; color: white; font-weight: 700; }
.sidebar { background: #e8eef5; }
.sidebar a { display: block; padding: 0.25rem 0; }
.site-footer { background: #1f3a5f; color: white; }
.card { border: 1px solid #9fb3c8; border-radius: 0.5rem; padding: 1rem; background: white; }
.card h2 { margin: 0 0 0.5rem; font-size: 1.1rem; }
.card p { margin: 0; }
CSS

Check it worked

Open the page in your browser. From a terminal on a desktop machine, xdg-open index.html on Linux or open index.html on macOS does it, or drag the file into a browser window. The regions are stacked in a single column, one under the other, because every element is a normal block. That is what you are about to change.

Step 2 · Lay out the page with named grid areas

A grid container is any element with display: grid. Its direct children become grid items. You describe the columns and rows, then give each region a name and place it by name. grid-template-areas draws the layout as text, one quoted string per row:

Bash
cat >> style.css <<'CSS'

.page {
  display: grid;
  min-height: 100vh;
  grid-template-columns: 14rem 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header  header"
    "sidebar main"
    "footer  footer";
  gap: 1rem;
}
.site-header { grid-area: header; }
.sidebar     { grid-area: sidebar; }
.content     { grid-area: main; }
.site-footer { grid-area: footer; }
CSS

Read it as a picture. There are two columns: a fixed 14rem one and a 1fr one. The fr unit means a share of the free space, so 1fr takes everything left after the fixed column and the gaps. There are three rows: auto for the header, which is as tall as its content, 1fr for the middle, which takes the rest of the height, and auto for the footer. The header and footer names each appear twice in a row, so those regions span both columns. grid-area: header on the element puts it there. gap: 1rem puts space between tracks and nowhere else, so no margin tricks are needed.

Because .page is at least 100vh tall and the middle row is 1fr, the footer sits at the bottom of the window even when the content is short.

Check it worked

Reload the page. The header runs across the top, the sidebar and the main area sit side by side, and the footer is at the bottom of the window. The sidebar is 224 pixels wide, which is 14rem when the browser's text size is 16 pixels.

Troubleshooting

If nothing changed, check that style.css is saved in the same folder and that the page was reloaded without a cached copy (Ctrl+Shift+R, or Cmd+Shift+R on macOS). If the areas look wrong, count the names: every row of grid-template-areas must have the same number of cells, and a name must form a rectangle. A name used in two separate places is invalid and the whole declaration is ignored.

Step 3 · Put the cards in a grid

The cards live inside .content. A second, nested grid arranges them. Start with three equal columns:

Bash
cat >> style.css <<'CSS'

.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}
CSS

repeat(3, 1fr) is shorthand for 1fr 1fr 1fr. The six cards fill the first row, then a second row appears on its own, because the browser creates extra rows for items that do not fit the grid you described. Those extra rows form the implicit grid, and grid-auto-rows sets their height if you need to control it.

Check it worked

Reload. The six cards sit in two rows of three, with equal widths and a 16-pixel gap. Make the window narrower. The columns shrink, and on a phone-sized window the cards become too squashed to read. The next two steps fix that.

Step 4 · Make the grid adjust its own column count

Fixed columns are the problem on small screens. Replace them with a rule that fits as many columns as there is room for, each at least a minimum width. Add this after the earlier .cards rule. Because a later rule with the same selector wins, it replaces the three-column setting. Delete the earlier grid-template-columns line when you tidy up:

Bash
cat >> style.css <<'CSS'

.cards {
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 12rem), 1fr));
}
CSS

Taken from the inside out. min(100%, 12rem) is the smaller of the container's width and 12rem, so a column can never be wider than the container, even on a very narrow screen. minmax(…, 1fr) says each column is at least that wide and may grow to take an equal share of the space. repeat(auto-fit, …) creates as many such columns as fit and, when there are fewer items than columns, collapses the empty columns so the items stretch to fill the row. Its sibling auto-fill keeps the empty columns, so items stay at their minimum size instead. This one rule replaces most of what media queries used to do for card grids.

Check it worked

Reload and drag the window edge. With a wide window you get several columns, then three, two and finally one as the window narrows, and the cards reflow without a media query. On a 1,000-pixel-wide window you should see three columns beside the sidebar. At 400 pixels you see one.

Step 5 · Make one card span two columns

Items normally take one cell. grid-column places an item along the column lines, and span 2 makes it cover two tracks. Give the first card more room:

Bash
cat >> style.css <<'CSS'

.card.featured {
  grid-column: span 2;
  background: #fff7e0;
}
CSS

The browser's auto-placement then fills the remaining cells around it. Spanning a card only makes sense when there are at least two columns. On a one-column grid, a span of two makes the browser invent a second column, so a phone gets two cramped columns of cards. The next step limits the span to the screens that have room.

Check it worked

Reload with a wide window. The "Getting started" card is twice as wide as the others and has a yellow background. Narrow the window to phone width and the cards are squeezed into two thin columns, which the next step fixes.

Step 6 · Change the layout on narrow screens

On a phone, a 14rem sidebar next to the main area leaves little room for content. Use a media query to stack everything in one column and put the sidebar after the main content. Because the layout is a picture in grid-template-areas, changing it means drawing a different picture. The same rule stops the featured card from spanning:

Bash
cat >> style.css <<'CSS'

@media (max-width: 40rem) {
  .page {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    grid-template-areas:
      "header"
      "main"
      "sidebar"
      "footer";
  }
  .card.featured { grid-column: auto; }
}
CSS

grid-template-rows: auto replaces the three-row definition, since there are now four stacked rows that all size to their content, with the extra rows created implicitly. The breakpoint 40rem is 640 pixels at the default text size. It is written in rem so that the layout also responds to users who raise their browser's font size.

Check it worked

Make the window narrower than 640 pixels, or use your browser's responsive device mode. The header is on top, the main content comes next, then the sidebar, then the footer, and all of them are one column wide. Check the layout with the browser's own tools, as described in the next step.

Step 7 · Inspect a grid in the browser

Your browser's developer tools draw the grid for you. In Chrome and Edge, open DevTools (F12), choose the Elements panel and click the small grid badge next to an element with display: grid in the HTML. The tracks, gaps and line numbers appear over the page. Firefox has the same badge and a Layout panel with extra options such as showing area names, and Safari's Web Inspector offers a similar overlay. Switch it on for .page and .cards and watch the lines move as you resize.

Two troubleshooting habits will save you time. If an item is in the wrong place, check whether it is a direct child of the grid container, because grandchildren are not grid items. And if a track is wider than you expected, look at the content: a grid item has a minimum size of its content, so a long word or an image with a fixed width stretches its 1fr column unless you write minmax(0, 1fr).

Step 8 · Clean up

Remove the demo folder:

Bash
cd ~
rm -rf ~/grid-demo

Next steps

  • Grid arranges the page. To style it for both light and dark preferences, add dark mode with CSS variables.
  • Combine Grid with a utility-first toolkit: set up Tailwind CSS 4 with Vite.
  • MDN's guide to CSS Grid layout covers subgrid, which lets nested grids share their parent's tracks, and the grid-auto-flow: dense option.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Digital Transformation

Get new tutorials

New tutorials, about twice a month.