7 / 9 · Basic
Grid basics
grid-template-columns: repeat(3, 1fr) makes three equal columns in one line, and the browser places every child into the next free cell.
Jump to the 10 practice questionsYou will learn
- Draw columns and rows with grid-template-columns and fr
- Place and span items, and name areas
- Let columns fit the screen with auto-fill and minmax
Before this: Flexbox
1Explain
Rows and columns at once
Grid, the CSS Grid Layout module, is built for two-dimensional layout: rows and columns together. You draw the grid on the container and the items fall into its cells. Where flexbox starts from the items and lets them work out their sizes, grid starts from the layout and fits the items into it.
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr); /* three equal columns */
gap: 10px;
}
.gallery .wide { grid-column: span 2; } /* this item covers two columns */2Explain
Tracks and the fr unit
grid-template-columns lists the width of each column, called a track. You can use any length, such as 200px 1fr, but grid adds a unit of its own: fr, one fraction of the free space. 1fr 1fr 1fr makes three equal columns; 2fr 1fr makes the first column twice as wide as the second. The fr unit shares out only the space that is left after fixed tracks and gaps, so 200px 1fr gives a 200 pixel sidebar and lets the main column take the rest.
repeat(3, 1fr) with a 20px gap. The numbers along the bottom are the grid lines, 1 to 4. The green item spans two columns, from line 1 to line 3.
3Explore
Column templates
fr shares the space left after fixed tracks and gaps. The last template makes as many 80px-or-wider columns as fit.
Tap a choice: the code and the preview change together.
<style>
.g { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.g div { background: #e8eefa; padding: 16px 4px; text-align: center; }
</style>
<div class="g"><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div></div>Nothing is drawn: there is no visible content.
4Problem
1fr 2fr in 600px
A 600px grid has columns 1fr 2fr and no gap. How wide is the second column?
px
Hint
3 shares in total.
Solution
Answer: 400 px
3 shares in total; 600 times 2/3 = 400.
5Problem
200px 1fr with a gap
A 700px grid has columns 200px 1fr and a 20px gap. How wide is the 1fr column?
px
Hint
Take the fixed column and the gap away first.
Solution
Answer: 480 px
fr shares what is left: 700 minus 200 minus 20 = 480.
6Problem
grid-column: 1 / -1
By default, items fill the grid in source order, one cell each, left to right and then onto the next row. To make an item bigger, grid-column: span 2 lets it cover two columns, and grid-row: span 2 two rows. The lines between tracks are numbered from 1, so a three-column grid has lines 1 to 4, and grid-column: 1 / 3 means from line 1 to line 3.
What does grid-column: 1 / -1 do?
Hint
-1 is the last grid line.
Solution
Answer: Stretches the item across all columns
From the first line to the last line; negative numbers count from the end.
7Explain
Naming areas
For a page layout, grid-template-areas lets you draw the layout with words. Each quoted string is one row and each word is one cell; repeating a word makes an area span those cells. Then each item gets grid-area with the matching name. The layout below reads almost like a sketch: the header across the top, a sidebar and the main content side by side, and the footer at the bottom.
.page {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-areas:
"head head"
"side main"
"foot foot";
gap: 16px;
}
header { grid-area: head; }
aside { grid-area: side; }
main { grid-area: main; }
footer { grid-area: foot; }8Problem
Assigning a named area
Hint
The property name ends in -area.
Solution
Answer: grid-area
grid-area on an item names the area it occupies. Setting grid-area: header places the item in the header area declared by the container.
9Practice
A gallery grid
Make .gallery a grid with three equal columns (repeat(3, 1fr)) and a 10px gap. Make .wide span two columns.
- grid container
- fr columns
- 10px gap
- wide item spans 2
<style>
.gallery div { background: #e8eefa; padding: 24px 8px; text-align: center; }
</style>
<div class="gallery">
<div class="wide">Sourdough</div>
<div>Rye</div>
<div>Baguette</div>
<div>Brioche</div>
<div>Focaccia</div>
</div>Hint
repeat(3, 1fr) makes three equal columns; span 2 makes .wide two columns wide.
A solution
<style>
.gallery div { background: #e8eefa; padding: 24px 8px; text-align: center; }
.gallery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.wide { grid-column: span 2; }
</style>
<div class="gallery">
<div class="wide">Sourdough</div>
<div>Rye</div>
<div>Baguette</div>
<div>Brioche</div>
<div>Focaccia</div>
</div>10Practice
A page drawn with words
Add grid-template-areas to .page with three rows: "head head", "side main" and "foot foot". Then give header, aside, main and footer the matching grid-area names head, side, main and foot.
- three rows of areas
- header in head
- aside in side
- main in main
- footer in foot
<style>
.page > * { background: #fff4d6; padding: 8px; }
.page {
display: grid;
grid-template-columns: 120px 1fr;
gap: 8px;
}
</style>
<div class="page">
<header>Corner Bakery</header>
<aside>Hours</aside>
<main>Today we bake rye.</main>
<footer>12 Mill Street</footer>
</div>Hint
The container gets the drawing; each child gets its name with grid-area.
A solution
<style>
.page > * { background: #fff4d6; padding: 8px; }
.page {
display: grid;
grid-template-columns: 120px 1fr;
gap: 8px;
grid-template-areas: "head head" "side main" "foot foot";
}
header { grid-area: head; }
aside { grid-area: side; }
main { grid-area: main; }
footer { grid-area: foot; }
</style>
<div class="page">
<header>Corner Bakery</header>
<aside>Hours</aside>
<main>Today we bake rye.</main>
<footer>12 Mill Street</footer>
</div>11Explain
Common mistakes
<style>
.p { display: grid; grid-template-columns: 100px 1fr; grid-template-areas: "head head" "side main" "foot"; }
.p > * { background: #fff4d6; }
header { grid-area: head; } aside { grid-area: side; } main { grid-area: main; } footer { grid-area: foot; }
</style>
<div class="p"><header>Head</header><aside>Side</aside><main>Main</main><footer>Foot</footer></div><style>
.p { display: grid; grid-template-columns: 100px 1fr; grid-template-areas: "head head" "side main" "foot foot"; }
.p > * { background: #fff4d6; }
header { grid-area: head; } aside { grid-area: side; } main { grid-area: main; } footer { grid-area: foot; }
</style>
<div class="p"><header>Head</header><aside>Side</aside><main>Main</main><footer>Foot</footer></div>Every row of grid-template-areas must have the same number of cells. One short row makes the whole value invalid.
12
Summary
- display: grid plus grid-template-columns draws the tracks; fr shares the free space.
- Items fill cells in order; span and line numbers place them.
- grid-template-areas draws a page layout with names; repeat(auto-fill, minmax()) fits columns to the screen.