An optional lesson. Students use CSS Grid to lay out a page in columns (a menu and the main content) and a gallery in rows and columns, and choose between Grid and Flexbox.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 10 min · Make 10 min · Gallery 5 min
This lesson is optional. It builds on Flexbox (lesson 7): Grid also goes on the parent, but it lines items up in columns and rows. Only three ideas are new: display: grid;, grid-template-columns (a list of widths, such as 150px 1fr) and repeat(3, 1fr) for equal columns. gap is the same as in Flexbox.
fr means one share of the space that is left after fixed widths. 1fr 2fr gives the second column twice the first. Items fill the columns in order and start a new row when a row is full. Do the Make task yourself first.
Reading grid-template-columns and what 1fr means. Ask them to predict each column's width before looking, then check with the page.
| What you will see | A question to ask | The fix |
|---|---|---|
| The menu and the main content still stack. | Which rule has display grid? Is it on the box that holds them? | Put display grid and grid-template-columns in the .page rule, not on nav or main. |
| Everything is in one column even with display grid. | Did you list the columns with grid-template-columns? | Add grid-template-columns, for example 160px 1fr or repeat(3, 1fr). |
| The columns are the wrong widths. | How many widths are listed, and which ones are fixed? | List one width per column; use fr for columns that share the space left over. |
One possible solution for each task. Other correct programs are fine.
<style>
nav {
background-color: lightblue;
padding: 8px;
}
.card {
padding: 12px;
border: 2px solid teal;
}
.page {
display: grid;
grid-template-columns: 160px 1fr;
}
</style>
<header>
<h1>Chess club</h1>
</header>
<div class="page">
<nav>
<p><a href="https://example.com/home">Home</a></p>
<p><a href="https://example.com/join">Join</a></p>
</nav>
<main>
<h2>Groups</h2>
<div class="cards">
<div class="card">Beginners</div>
<div class="card">Players</div>
<div class="card">Champions</div>
<div class="card">Coaches</div>
<div class="card">Parents</div>
<div class="card">Visitors</div>
</div>
</main>
</div>
<style>
nav {
background-color: lightblue;
padding: 8px;
}
.card {
padding: 12px;
border: 2px solid teal;
}
.page {
display: grid;
grid-template-columns: 160px 1fr;
gap: 16px;
}
</style>
<header>
<h1>Chess club</h1>
</header>
<div class="page">
<nav>
<p><a href="https://example.com/home">Home</a></p>
<p><a href="https://example.com/join">Join</a></p>
</nav>
<main>
<h2>Groups</h2>
<div class="cards">
<div class="card">Beginners</div>
<div class="card">Players</div>
<div class="card">Champions</div>
<div class="card">Coaches</div>
<div class="card">Parents</div>
<div class="card">Visitors</div>
</div>
</main>
</div>
<style>
nav {
background-color: lightblue;
padding: 8px;
}
.card {
padding: 12px;
border: 2px solid teal;
}
.page {
display: grid;
grid-template-columns: 160px 1fr;
gap: 16px;
}
.cards {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
}
</style>
<header>
<h1>Chess club</h1>
</header>
<div class="page">
<nav>
<p><a href="https://example.com/home">Home</a></p>
<p><a href="https://example.com/join">Join</a></p>
</nav>
<main>
<h2>Groups</h2>
<div class="cards">
<div class="card">Beginners</div>
<div class="card">Players</div>
<div class="card">Champions</div>
<div class="card">Coaches</div>
<div class="card">Parents</div>
<div class="card">Visitors</div>
</div>
</main>
</div>
<style>
.page {
display: grid;
grid-template-columns: 1fr 200px;
gap: 20px;
}
main {
background-color: lavender;
padding: 12px;
}
.side {
background-color: lightyellow;
padding: 12px;
}
</style>
<div class="page">
<main>
<h1>Music night</h1>
<p>Friday at 7 pm in the hall. Bands, choirs and solo acts.</p>
</main>
<div class="side">
<h2>Tickets</h2>
<p>Free, from the office.</p>
</div>
</div>
<style>
.photo {
padding: 8px;
border: 1px solid gray;
text-align: center;
}
.photos {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
width: 420px;
}
</style>
<h1>Our year in pictures</h1>
<div class="photos">
<div class="photo"><img src="ball.svg" alt="Soccer final" width="70"><p>Soccer final</p></div>
<div class="photo"><img src="music.svg" alt="Music night" width="70"><p>Music night</p></div>
<div class="photo"><img src="book.svg" alt="Book week" width="70"><p>Book week</p></div>
<div class="photo"><img src="star.svg" alt="Awards" width="70"><p>Awards</p></div>
<div class="photo"><img src="trophy.svg" alt="Trophy day" width="70"><p>Trophy day</p></div>
<div class="photo"><img src="medal.svg" alt="Sports day" width="70"><p>Sports day</p></div>
</div>
Stretch: Make one card span two columns with grid-column: span 2; or change the gallery to repeat(auto-fill, minmax(120px, 1fr)) so the number of columns follows the width.
Support: Do the first Modify task together. Keep the hint open for the cards: it is the same as the page rule with repeat(3, 1fr).
Write the rule for a gallery box with four equal columns and a gap, and say why it goes on the gallery, not on each picture.
.gallery { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; } It goes on the box that holds the pictures, because Grid arranges that box's children.
Back in the club page, students can use Grid for the cards or the whole page. The DOM course and later web projects use both Grid (for page layout) and Flexbox (inside each part).