Aral Code

Layout Lesson 22 of 33

CSS Grid Template Areas — Drawing Page Layouts

Name the parts of your page and "draw" the layout with grid-template-areas, align grid items, and build a complete responsive page layout.

In the last lesson, you placed grid items using numbers. Grid has an even friendlier way to build a page layout: you name each part (header, sidebar, main, footer) and then literally draw the layout with words.

Naming areas

First, give each item a name with grid-area:

header { grid-area: header; }
nav    { grid-area: nav; }
main   { grid-area: main; }
footer { grid-area: footer; }

Drawing the layout

Then, in the container, grid-template-areas lists the names in quotes, one string per row. Each word is one cell. Repeating a name makes that area span across cells:

Example A page layout drawn with words
<style>
  * { box-sizing: border-box; }
  body { margin: 0; font-family: Arial, sans-serif; }
  .page {
    display: grid;
    grid-template-columns: 160px 1fr;
    grid-template-rows: auto 1fr auto;
    grid-template-areas:
      "header header"
      "nav    main"
      "footer footer";
    min-height: 100vh;
    gap: 8px;
    padding: 8px;
  }
  header { grid-area: header; background: #0f5c4d; color: white; }
  nav    { grid-area: nav; background: #e3f1ec; }
  main   { grid-area: main; background: #fff; border: 1px solid #ddd; }
  footer { grid-area: footer; background: #1b2421; color: white; }
  header, nav, main, footer { padding: 12px; border-radius: 8px; }
</style>
<div class="page">
  <header>🏫 San Isidro National High School</header>
  <nav>News<br>Events<br>Enrollment</nav>
  <main>Main content goes here.</main>
  <footer>© 2026 SINHS</footer>
</div>

Read the grid-template-areas value like a picture of the page. The header spans both columns on top, nav and main sit side by side in the middle, and the footer spans the bottom. Want the sidebar on the right? Just swap the words: "main nav". Try it!

Responsive layout: change the drawing

Here's the best part. On small screens, you redraw the layout as a single column. You change only the container's rule; the HTML stays the same. This uses a media query, which you'll study fully in the next lesson. For now, read it as "when the screen is 600px wide or less":

Example Two columns on wide screens, one on phones
<style>
  * { box-sizing: border-box; }
  body { margin: 0; font-family: Arial, sans-serif; }
  .page {
    display: grid;
    gap: 8px;
    padding: 8px;
    grid-template-columns: 1fr 200px;
    grid-template-areas:
      "header header"
      "main   aside"
      "footer footer";
  }
  @media (max-width: 600px) {
    .page {
      grid-template-columns: 1fr;
      grid-template-areas:
        "header"
        "main"
        "aside"
        "footer";
    }
  }
  header { grid-area: header; background: #0f5c4d; color: white; }
  main   { grid-area: main; background: #f3f6f4; min-height: 120px; }
  aside  { grid-area: aside; background: #fdf3dc; }
  footer { grid-area: footer; background: #1b2421; color: white; }
  header, main, aside, footer { padding: 12px; border-radius: 8px; }
</style>
<div class="page">
  <header>Kusina ni Kuya — Recipe Blog</header>
  <main><strong>Chicken Tinola</strong><br>A warm ginger soup with green papaya and malunggay.</main>
  <aside>Popular: Adobo, Sinigang, Lumpia</aside>
  <footer>Salamat sa pagbisita!</footer>
</div>

Make the editor narrow (or open the page on your phone) and the sidebar moves under the main content.

Aligning items inside their cells

By default, grid items stretch to fill their cell. You can change this:

  • justify-items — horizontal alignment of all items in their cells (start, center, end, stretch)
  • align-items — vertical alignment of all items
  • place-items: center — shorthand for both. With just one item, it centers it perfectly!
  • justify-self / align-self — the same, for one item only
Example Centering with place-items
<style>
  .stage { display: grid; place-items: center; height: 200px; background: #2f6fd6; border-radius: 12px; }
  .badge { background: white; padding: 14px 22px; border-radius: 999px; font: bold 18px Arial, sans-serif; }
</style>
<div class="stage"><span class="badge">🎓 Congratulations, graduates!</span></div>

display: grid; place-items: center; is the shortest way to center something in CSS, even shorter than Flexbox.

A dashboard example

Template areas make dashboards easy to read and change:

Example A sales dashboard layout
<style>
  .dash {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-areas:
      "today week  month"
      "chart chart top"
      "chart chart top";
    gap: 10px;
    font-family: Arial, sans-serif;
  }
  .dash > div { background: white; border: 1px solid #e3e0d6; border-radius: 12px; padding: 12px; }
  .today { grid-area: today; } .week { grid-area: week; } .month { grid-area: month; }
  .chart { grid-area: chart; min-height: 160px; background: linear-gradient(#e3f1ec, #fff) !important; }
  .top { grid-area: top; }
  .big { font-size: 22px; font-weight: bold; color: #0f5c4d; }
</style>
<div class="dash">
  <div class="today">Today<div class="big">₱2,340</div></div>
  <div class="week">This week<div class="big">₱15,880</div></div>
  <div class="month">This month<div class="big">₱61,200</div></div>
  <div class="chart">📈 Sales chart</div>
  <div class="top">Top sellers:<br>1. Turon<br>2. Banana cue<br>3. Buko juice</div>
</div>

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. In grid-template-areas, how do you make the header span two columns?
2. Which property gives an element its area name?
3. Which of these areas is NOT allowed?
4. What does place-items: center do in a grid container?

By Sir Reyel · Last updated 2026-12-04