From Zero to Senior · The LabالعربيةGitHub

Chapter 7

Layout with Flexbox and Grid

6 examples · 3 projects · 5 exercises

In this chapter you will learn to

  • place elements in a row or a column with Flexbox
  • space, align and centre elements in both directions
  • build two-dimensional layouts with CSS Grid
  • make pages adapt to every screen with media queries
  • give your profile page a real, responsive layout

Examples

7.1 Flexbox: a row of boxes

Normally block elements stack from top to bottom. Add display: flex to a parent element, and its children line up in a row instead. The parent is called the flex container, and its direct children are flex items.

flex.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Flexbox</title>
    <style>
      body { font-family: system-ui, sans-serif; margin: 16px; }
      .bar {
        display: flex;
        gap: 8px;
        padding: 8px;
        margin-bottom: 4px;
        background-color: #eef2ff;
        border-radius: 10px;
      }
      .bar a {
        padding: 6px 12px;
        border-radius: 8px;
        color: white;
        background-color: #4338ca;
        text-decoration: none;
      }
      .start { justify-content: flex-start; }
      .between { justify-content: space-between; }
      .centre { justify-content: center; }
      code { display: block; margin: 12px 0 4px; color: #5b5873; }
    </style>
  </head>
  <body>
    <code>justify-content: flex-start</code>
    <nav class="bar start"><a href="#">Home</a><a href="#">Articles</a><a href="#">Videos</a><a href="#">About</a></nav>
    <code>justify-content: space-between</code>
    <nav class="bar between"><a href="#">Home</a><a href="#">Articles</a><a href="#">Videos</a><a href="#">About</a></nav>
    <code>justify-content: center</code>
    <nav class="bar centre"><a href="#">Home</a><a href="#">Articles</a><a href="#">Videos</a><a href="#">About</a></nav>
  </body>
</html>
flex.html560px
Figure 7.1: The same menu with three values of justify-content.

7.2 Centring anything

Put justify-content: center and align-items: center together, and an item sits exactly in the middle of its container, horizontally and vertically:

center.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Centring</title>
    <style>
      body {
        margin: 0;
        min-height: 260px;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #eef2ff;
        font-family: system-ui, sans-serif;
      }
      .card {
        padding: 24px 32px;
        border-radius: 16px;
        color: white;
        background-color: #db2777;
        font-size: 1.4rem;
        font-weight: 700;
      }
    </style>
  </head>
  <body>
    <div class="card">Perfectly centred!</div>
  </body>
</html>
center.html560px
Figure 7.3: Centred in both directions.

7.3 Grid: rows and columns together

Flexbox arranges items along one line. CSS Grid arranges them in two dimensions at once, like a table or a spreadsheet. Set display: grid on the container and describe its columns:

grid.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Grid gallery</title>
    <style>
      body { font-family: system-ui, sans-serif; margin: 16px; }
      .gallery {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        gap: 12px;
      }
      .gallery div {
        padding: 28px 8px;
        text-align: center;
        border-radius: 12px;
        color: white;
        background-color: #7c3aed;
        font-weight: 700;
      }
    </style>
  </head>
  <body>
    <div class="gallery">
      <div>HTML</div>
      <div>CSS</div>
      <div>JavaScript</div>
      <div>Git</div>
      <div>Node.js</div>
      <div>DevTools</div>
    </div>
  </body>
</html>
grid.html560px
Figure 7.4: A gallery: as many 140-pixel-or-wider columns as fit.

7.4 Page layouts with named areas

Grid can also lay out a whole page. You give each part a name and then “draw” the layout with those names, almost like ASCII art:

layout.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Page layout</title>
    <style>
      body { font-family: system-ui, sans-serif; margin: 12px; }
      .page {
        display: grid;
        grid-template-columns: 180px 1fr;
        grid-template-areas:
          "head head"
          "side main"
          "foot foot";
        gap: 10px;
      }
      .page > * { padding: 20px; border-radius: 10px; color: white; font-weight: 700; }
      header { grid-area: head; background-color: #4338ca; }
      aside  { grid-area: side; background-color: #0f766e; }
      main   { grid-area: main; background-color: #db2777; min-height: 120px; }
      footer { grid-area: foot; background-color: #ea580c; }

      /* On narrow screens: one column */
      @media (max-width: 600px) {
        .page {
          grid-template-columns: 1fr;
          grid-template-areas: "head" "main" "side" "foot";
        }
      }
    </style>
  </head>
  <body>
    <div class="page">
      <header>Header</header>
      <aside>Sidebar</aside>
      <main>Main content</main>
      <footer>Footer</footer>
    </div>
  </body>
</html>
layout.html760px
Figure 7.5: The page layout on a wide screen.
Figure 7.6: The same page and the same HTML at phone width.

7.6 Project: a responsive profile

Your profile page is beautiful but still a single tall column. Let us add a sixth part to style.css with a real layout. Nothing in the HTML changes.

layout-step.css⤓ File
/* 6. Chapter 7: layout with Flexbox and Grid */
*,
*::before,
*::after {
  box-sizing: border-box;
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px 24px;
}

header nav {
  flex-basis: 100%;
}

main {
  max-width: 900px;
  display: grid;
  gap: 16px;
}

section {
  margin-bottom: 0;
}

ul {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

li {
  margin: 0;
}

/* Wide screens: avatar beside the name, cards in two columns */
@media (min-width: 700px) {
  header {
    justify-content: flex-start;
    text-align: start;
    padding-inline: 48px;
  }

  main {
    grid-template-columns: 1fr 1fr;
  }

  #about {
    grid-column: 1 / -1;
  }
}

Your profile page is beautiful but still a single tall column. Let us add a sixth part to style.css with a real layout. Nothing in the HTML changes.

profile.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="style.css">
    <title>About Sara</title>
  </head>
  <body>
    <header>
      <img src="bit.svg" alt="Sara’s avatar: a smiling purple robot" width="90" height="105">
      <h1>Sara Khan</h1>
      <p>Beginner web developer from Alexandria</p>
      <nav>
        <a href="#about">About</a> |
        <a href="#skills">Skills</a> |
        <a href="#contact">Contact</a>
      </nav>
    </header>

    <main>
      <section id="about">
        <h2>About me</h2>
        <p>I started learning to code this year with <em>Code from Zero</em>. I love building small tools that make life easier.</p>
      </section>

      <section id="skills">
        <h2>Skills I am learning</h2>
        <ul>
          <li>HTML</li>
          <li>CSS</li>
          <li>JavaScript</li>
        </ul>
      </section>

      <section id="contact">
        <h2>Contact</h2>
        <p>Write to me at <a href="mailto:sara@example.com">sara@example.com</a>.</p>
      </section>
    </main>

    <footer>
      <p>Built by Sara, with HTML and CSS.</p>
    </footer>
  </body>
</html>
profile.html760px
Figure 7.7: On a wide screen: avatar beside the name, cards in two columns.
Figure 7.8: On a phone: one column, everything centred. Same HTML, same CSS file.
+ 2 files
bit.svg⤓ File
style.css⤓ File
/* 1. Colours and fonts for the whole page */
:root {
  --brand: #4338ca;
  --accent: #db2777;
  --soft: #eef2ff;
}

body {
  margin: 0;
  font-family: system-ui, sans-serif;
  line-height: 1.6;
  color: #1e1b2e;
  background-color: #f5f5fb;
}

/* 2. The header: a coloured band with the avatar */
header {
  text-align: center;
  padding: 32px 16px 24px;
  color: white;
  background-color: var(--brand);
}

header img {
  background-color: white;
  border-radius: 50%;
  padding: 8px;
}

header h1 {
  margin: 8px 0 0;
  font-size: 2rem;
}

header p {
  margin: 0 0 16px;
  opacity: 0.85;
}

/* 3. The menu: links that look like buttons */
nav a {
  display: inline-block;
  padding: 6px 16px;
  margin: 4px;
  border-radius: 999px;
  color: var(--brand);
  background-color: white;
  text-decoration: none;
  font-weight: 700;
}

/* 4. The content: white cards, centred */
main {
  max-width: 640px;
  margin: 24px auto;
  padding: 0 16px;
}

section {
  background-color: white;
  border-radius: 16px;
  padding: 8px 24px 16px;
  margin-bottom: 16px;
  box-shadow: 0 2px 8px rgba(30, 27, 46, 0.08);
}

h2 {
  color: var(--brand);
  border-bottom: 3px solid var(--soft);
  padding-bottom: 4px;
}

/* 5. Skills as coloured tags */
ul {
  padding: 0;
}

li {
  display: inline-block;
  list-style: none;
  padding: 4px 14px;
  margin: 4px;
  border-radius: 999px;
  color: white;
  background-color: var(--accent);
}

a {
  color: var(--accent);
}

footer {
  text-align: center;
  color: #5b5873;
  font-size: 0.9rem;
  padding-bottom: 24px;
}

/* 6. Chapter 7: layout with Flexbox and Grid */
*,
*::before,
*::after {
  box-sizing: border-box;
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px 24px;
}

header nav {
  flex-basis: 100%;
}

main {
  max-width: 900px;
  display: grid;
  gap: 16px;
}

section {
  margin-bottom: 0;
}

ul {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

li {
  margin: 0;
}

/* Wide screens: avatar beside the name, cards in two columns */
@media (min-width: 700px) {
  header {
    justify-content: flex-start;
    text-align: start;
    padding-inline: 48px;
  }

  main {
    grid-template-columns: 1fr 1fr;
  }

  #about {
    grid-column: 1 / -1;
  }
}

Practice projects

Project 7.1 · Easy

The café, side by side

Give the café website from Project 6.2 a real layout: on wide screens, its sections sit next to each other as cards.

You will practise: Grid with auto-fit and minmax, gap, and a wrapping Flexbox menu.

Steps
  1. Open cafe.css from Chapter 6 and add a new part at the end, with a comment: /* Chapter 7 */.
  2. Make main a grid with grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)) and a gap, and widen its max-width to 900 pixels.
  3. Remove the bottom margin of the sections: the gap now does the spacing.
  4. Make the nav a wrapping flex container, centred, with a gap between the pills.
  5. Resize the window slowly from wide to narrow, and watch the cards go from three columns to two to one.

Hint: you did not write a single media query, yet the layout adapts. That is the power of auto-fit with minmax.

Solution
cafe.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="cafe.css">
    <title>Bean & Book Café</title>
  </head>
  <body>
    <header>
      <h1>Bean &amp; Book Café</h1>
      <p>Coffee, cake and a quiet corner to read.</p>
      <nav>
        <a href="#menu">Menu</a> |
        <a href="#hours">Opening hours</a> |
        <a href="#find">Find us</a>
      </nav>
    </header>

    <main>
      <section id="menu">
        <h2>Menu</h2>
        <ul>
          <li>Coffee: 30</li>
          <li>Tea with mint: 15</li>
          <li>Chocolate cake: 45</li>
        </ul>
      </section>
      <section id="hours">
        <h2>Opening hours</h2>
        <ul>
          <li>Saturday to Thursday: 8:00 to 22:00</li>
          <li>Friday: 14:00 to 23:00</li>
        </ul>
      </section>
      <section id="find">
        <h2>Find us</h2>
        <p>12 Library Street, Alexandria. Call us on <a href="tel:+201000000000">010 0000 0000</a>.</p>
      </section>
    </main>

    <footer>
      <p>© 2026 Bean &amp; Book Café</p>
    </footer>
  </body>
</html>
cafe.html760px
Project 7.1: the café on a wide screen.
cafe.css⤓ File
:root {
  --brown: #6b4423;
  --cream: #fff8ee;
  --gold: #d97706;
}

body {
  margin: 0;
  font-family: system-ui, "Segoe UI", Tahoma, sans-serif;
  color: #2b2118;
  background-color: var(--cream);
  line-height: 1.6;
}

header {
  text-align: center;
  padding: 32px 16px;
  color: white;
  background-color: var(--brown);
}

header h1 {
  margin: 0;
}

nav a {
  display: inline-block;
  margin: 4px;
  padding: 6px 14px;
  border-radius: 999px;
  color: var(--brown);
  background-color: var(--cream);
  text-decoration: none;
  font-weight: 700;
}

main {
  max-width: 560px;
  margin: 24px auto;
  padding: 0 16px;
}

section {
  background-color: white;
  border-radius: 14px;
  padding: 4px 20px 12px;
  margin-bottom: 16px;
  border-top: 4px solid var(--gold);
}

h2 {
  color: var(--brown);
}

a {
  color: var(--gold);
}

footer {
  text-align: center;
  font-size: 0.9rem;
  padding: 16px;
  color: var(--brown);
}

/* Chapter 7: sections side by side on wide screens */
main {
  max-width: 900px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
}

section {
  margin-bottom: 0;
}

nav {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

Project 7.2 · Medium

A “Meet the team” page

Build a grid of team member cards, each with a round avatar, a name and a role, perfectly centred inside the card.

You will practise: Grid for the page, Flexbox inside each card, and centring in both directions.

Steps
  1. In team.html, write a <main class="team"> containing six <article class="person">, each with a <div class="avatar"> (the first letter of the name), an <h2> and a <p>.
  2. Make .team a responsive grid of cards at least 150 pixels wide.
  3. Make each .person a flex column with align-items: center, so everything inside is centred.
  4. Make .avatar a 64-pixel circle (border-radius: 50%) and centre its letter with Flexbox: justify-content and align-items both set to center.
  5. Check the page at 360 pixels: how many cards per row?

Hint: this is the “Flexbox inside Grid” pattern from the Tip in Section 7.5: Grid places the cards; Flexbox arranges what is inside each card.

Solution
team.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Team</title>
    <link rel="stylesheet" href="team.css">
  </head>
  <body>
    <h1>Meet the team</h1>
    <main class="team">
      <article class="person">
        <div class="avatar">S</div>
        <h2 style="margin:0;font-size:1.1rem">Sara</h2>
        <p>Frontend</p>
      </article>
      <article class="person">
        <div class="avatar">O</div>
        <h2 style="margin:0;font-size:1.1rem">Omar</h2>
        <p>Backend</p>
      </article>
      <article class="person">
        <div class="avatar">H</div>
        <h2 style="margin:0;font-size:1.1rem">Huda</h2>
        <p>Design</p>
      </article>
      <article class="person">
        <div class="avatar">A</div>
        <h2 style="margin:0;font-size:1.1rem">Adam</h2>
        <p>Mobile</p>
      </article>
      <article class="person">
        <div class="avatar">L</div>
        <h2 style="margin:0;font-size:1.1rem">Lina</h2>
        <p>Testing</p>
      </article>
      <article class="person">
        <div class="avatar">Y</div>
        <h2 style="margin:0;font-size:1.1rem">Youssef</h2>
        <p>DevOps</p>
      </article>
    </main>
  </body>
</html>
team.html560px
Project 7.2: a team grid with centred cards.
team.css⤓ File
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; padding: 20px; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; background: #f5f5fb; }
h1 { color: #4338ca; }
.team {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 16px;
}
.person {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 18px 10px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 2px 8px rgba(30, 27, 46, 0.08);
}
.avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 1.6rem;
  font-weight: 700;
  color: white;
  background: #7c3aed;
}
.person p { margin: 0; color: #5b5873; }

Project 7.3 · Challenge

A product landing page

Build the classic landing page that almost every product has: a top bar, a “hero” with text and a picture, and three feature cards. It must look right on phones too.

You will practise: space-between, flex: 1, fixed grid columns, and a media query that changes direction.

Steps
  1. Sketch the page first: a bar with the brand on one side and links on the other; a hero with text beside a picture; a row of three cards; a footer.
  2. Make the bar a flex container with justify-content: space-between, so the brand and the links are pushed to opposite sides.
  3. Make the hero a flex container. Give its text block flex: 1, so it takes all the space the picture does not need.
  4. Lay out the three features with grid-template-columns: repeat(3, 1fr).
  5. Add a media query for screens up to 600 pixels: the hero becomes a centred column (flex-direction: column) and the grid becomes one column.

Hint: the picture is decorative, so its alt is empty (alt=""). The heading already says everything that matters.

Solution
landing.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>TaskFlow</title>
    <link rel="stylesheet" href="landing.css">
  </head>
  <body>
    <header class="bar">
      <span class="brand">TaskFlow</span>
      <nav><a href="#">Features</a><a href="#">Pricing</a><a href="#">Sign in</a></nav>
    </header>
    <section class="hero">
      <div>
        <h1>Plan your day in minutes</h1>
        <p>TaskFlow keeps your tasks, notes and deadlines in one simple place.</p>
        <a class="button" href="#">Start for free</a>
      </div>
      <img src="bit.svg" alt="" width="180" height="210">
    </section>
    <section class="features">
      <h2>Why TaskFlow?</h2>
      <div class="grid">
        <article>
          <h3>Simple</h3>
          <p>Add a task in two seconds.</p>
        </article>
        <article>
          <h3>Everywhere</h3>
          <p>On your phone and your laptop.</p>
        </article>
        <article>
          <h3>Private</h3>
          <p>Your data stays yours.</p>
        </article>
      </div>
    </section>
    <footer>© 2026 TaskFlow</footer>
  </body>
</html>
landing.html380px
…and on a phone.
landing.css⤓ File
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; color: #1e1b2e; }

/* Top bar: brand on one side, links on the other */
.bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 24px;
  border-bottom: 1px solid #eef2ff;
}
.brand { font-weight: 800; color: #4338ca; font-size: 1.3rem; }
.bar nav { display: flex; gap: 16px; }
.bar a { color: #1e1b2e; text-decoration: none; }

/* Hero: text and picture side by side */
.hero {
  display: flex;
  align-items: center;
  gap: 32px;
  padding: 40px 24px;
  background: #eef2ff;
}
.hero div { flex: 1; }
.hero h1 { font-size: 2.2rem; margin: 0 0 8px; }
.button { display: inline-block; padding: 10px 22px; border-radius: 999px; color: white; background: #db2777; text-decoration: none; font-weight: 700; }
.hero img { width: 180px; height: auto; }

/* Features: three columns */
.features { padding: 24px; }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.grid article { padding: 16px; border-radius: 14px; background: #f5f5fb; }
.grid h3 { margin: 0 0 4px; color: #4338ca; }
footer { text-align: center; padding: 16px; color: #5b5873; }

/* Phones: everything in one column */
@media (max-width: 600px) {
  .hero { flex-direction: column; text-align: center; }
  .grid { grid-template-columns: 1fr; }
  .bar nav { gap: 10px; font-size: 0.9rem; }
}
+ 1 files
bit.svg⤓ File

Exercises

Exercise 7.1 · Easy

Make a row of three buttons with Flexbox, with 12 pixels between them, centred on the page.
The book's short answer
.buttons { display: flex; gap: 12px; justify-content: center; }
Show the solution
buttons.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Three buttons</title>
    <style>
      body { font-family: system-ui, sans-serif; margin: 24px; }
      /* A flex row: 12px between the buttons, centred on the page. */
      .buttons {
        display: flex;
        gap: 12px;
        justify-content: center;
      }
      .buttons button {
        padding: 10px 20px;
        border: 0;
        border-radius: 10px;
        color: white;
        background-color: #4338ca;
        font: inherit;
        font-weight: 700;
      }
    </style>
  </head>
  <body>
    <div class="buttons">
      <button type="button">Save</button>
      <button type="button">Share</button>
      <button type="button">Delete</button>
    </div>
  </body>
</html>
buttons.html640px

Exercise 7.2 · Easy

Change the gallery so that each column is at least 200 pixels wide. How many columns do you get at different window widths?
The book's short answer
minmax(200px, 1fr). With an 800-pixel window you get three columns, with 500 pixels two, and below 400 pixels one.
Show the solution

With minmax(200px, 1fr) each column is at least 200 pixels wide, so the number of columns is the page width divided by about 212 (200 plus the 12-pixel gap), rounded down:

Window width Columns
1280px 6
800px 3
500px 2
below 440px 1

Open gallery.html and resize the window: the page prints the width and the number of columns as you go.

gallery.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Grid gallery</title>
    <style>
      body { font-family: system-ui, sans-serif; margin: 16px; }
      .gallery {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 12px;
      }
      .gallery div {
        padding: 28px 8px;
        text-align: center;
        border-radius: 12px;
        color: white;
        background-color: #7c3aed;
        font-weight: 700;
      }
    </style>
  </head>
  <body>
    <div class="gallery">
      <div>HTML</div>
      <div>CSS</div>
      <div>JavaScript</div>
      <div>Git</div>
      <div>Node.js</div>
      <div>DevTools</div>
    </div>
    <!-- Shows the window width and the number of columns; resize the window and watch them change. -->
    <p id="info" style="margin-top: 16px"></p>
    <script>
      const show = () => {
        const columns = getComputedStyle(document.querySelector(".gallery")).gridTemplateColumns.split(" ").length;
        document.querySelector("#info").textContent = `Window: ${window.innerWidth}px · columns: ${columns}`;
      };
      show();
      window.addEventListener("resize", show);
    </script>
  </body>
</html>
gallery.html640px

Exercise 7.3 · Medium

Build a card for a product with an image on one side and text on the other, using Flexbox. On screens narrower than 500 pixels, put the image above the text.
The book's short answer
.card { display: flex; gap: 16px; } @media (max-width: 500px) { .card { flex-direction: column; } }
Show the solution
product.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Product card</title>
    <style>
      body { font-family: system-ui, sans-serif; margin: 24px; background: #f5f5fb; }
      .card {
        display: flex;
        gap: 16px;
        align-items: center;
        max-width: 560px;
        padding: 16px;
        border-radius: 16px;
        background: white;
        box-shadow: 0 2px 8px rgba(30, 27, 46, 0.1);
      }
      .card img { flex-shrink: 0; border-radius: 12px; background: #eef2ff; }
      .card h2 { margin: 0 0 4px; color: #4338ca; }
      .price { font-size: 1.4rem; font-weight: 700; color: #db2777; }

      /* Narrow screens: the picture goes above the text. */
      @media (max-width: 500px) {
        .card { flex-direction: column; text-align: center; }
      }
    </style>
  </head>
  <body>
    <article class="card">
      <img src="../../examples/bit.svg" alt="A Bit sticker: a smiling purple robot" width="140" height="160">
      <div>
        <h2>Bit sticker</h2>
        <p>A waterproof sticker for your laptop, 8 cm tall.</p>
        <p class="price">$3</p>
      </div>
    </article>
  </body>
</html>
product.html640px

Exercise 7.4 · Medium

Add a fourth area, ads, to layout.html, shown to the end of the main content on wide screens and at the very bottom on phones.
The book's short answer
Add <aside class="ads">, use "head head" "side main" "side ads" "foot foot" on wide screens, and "head" "main" "side" "foot" "ads" in the media query.
Show the solution

On wide screens the areas are "head head" "side main" "side ads" "foot foot": the sidebar spans two rows, and the ads sit at the end of the main content. In the media query they become "head" "main" "side" "foot" "ads", so on phones the ads come last, after the footer.

layout.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Page layout</title>
    <style>
      body { font-family: system-ui, sans-serif; margin: 12px; }
      .page {
        display: grid;
        grid-template-columns: 180px 1fr;
        grid-template-areas:
          "head head"
          "side main"
          "side ads"
          "foot foot";
        gap: 10px;
      }
      .page > * { padding: 20px; border-radius: 10px; color: white; font-weight: 700; }
      header { grid-area: head; background-color: #4338ca; }
      aside  { grid-area: side; background-color: #0f766e; }
      main   { grid-area: main; background-color: #db2777; min-height: 120px; }
      footer { grid-area: foot; background-color: #ea580c; }

      /* On narrow screens: one column */
      .ads   { grid-area: ads; background-color: #7c3aed; }

      @media (max-width: 600px) {
        .page {
          grid-template-columns: 1fr;
          grid-template-areas: "head" "main" "side" "foot" "ads";
        }
      }
    </style>
  </head>
  <body>
    <div class="page">
      <header>Header</header>
      <aside>Sidebar</aside>
      <main>Main content</main>
      <aside class="ads">Ads</aside>
      <footer>Footer</footer>
    </div>
  </body>
</html>
layout.html640px

Exercise 7.5 · Challenge

Recreate the layout of the home page of a news website you like, with Grid areas and coloured boxes instead of real content. Make it work at 360, 768 and 1280 pixels wide.
The book's short answer
A grid with named areas for the header, the main story, side stories and the footer, plus media queries at around 768 and 1024 pixels.
Show the solution
news.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>A news home page, in boxes</title>
    <style>
      body { font-family: system-ui, sans-serif; margin: 12px; }
      .page > * { padding: 16px; border-radius: 10px; color: white; font-weight: 700; min-height: 40px; }
      header { grid-area: head; background: #1e1b2e; }
      nav    { grid-area: nav;  background: #4338ca; }
      .lead  { grid-area: lead; background: #db2777; min-height: 220px; }
      .s1    { grid-area: s1;   background: #0f766e; }
      .s2    { grid-area: s2;   background: #0e7490; }
      .s3    { grid-area: s3;   background: #7c3aed; }
      .s4    { grid-area: s4;   background: #9333ea; }
      aside  { grid-area: side; background: #ea580c; }
      footer { grid-area: foot; background: #57534e; }

      /* Phones (360px): everything in one column, the main story first. */
      .page {
        display: grid;
        gap: 10px;
        grid-template-areas: "head" "nav" "lead" "s1" "s2" "s3" "s4" "side" "foot";
      }

      /* Tablets (768px): two columns, the main story across both. */
      @media (min-width: 768px) {
        .page {
          grid-template-columns: 1fr 1fr;
          grid-template-areas:
            "head head"
            "nav  nav"
            "lead lead"
            "s1   s2"
            "s3   s4"
            "side side"
            "foot foot";
        }
      }

      /* Desktops (1024px and up, so 1280px too): the main story is big, the side column runs down the right. */
      @media (min-width: 1024px) {
        .page {
          max-width: 1200px;
          margin: 0 auto;
          grid-template-columns: 1fr 1fr 1fr 280px;
          grid-template-areas:
            "head head head head"
            "nav  nav  nav  nav"
            "lead lead s1   side"
            "lead lead s2   side"
            "s3   s4   s4   side"
            "foot foot foot foot";
        }
      }
    </style>
  </head>
  <body>
    <div class="page">
      <header>The Daily Byte</header>
      <nav>Menu</nav>
      <article class="lead">Main story</article>
      <article class="s1">Story 1</article>
      <article class="s2">Story 2</article>
      <article class="s3">Story 3</article>
      <article class="s4">Story 4</article>
      <aside>Most read</aside>
      <footer>Footer</footer>
    </div>
  </body>
</html>
news.html640px