edubun
English
1 / 12

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 questions

You 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.

Your code
.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 */
Preview

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.

grid-template-columns
<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>
Preview

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.

Your code
.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; }
Preview

8Problem

Assigning a named area

Which property assigns an item to 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.

Checks
  • grid container
  • fr columns
  • 10px gap
  • wide item spans 2
Your code
<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>
Preview
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.

Checks
  • three rows of areas
  • header in head
  • aside in side
  • main in main
  • footer in foot
Your code
<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>
Preview
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

Mistake
<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>
Fixed
<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.

Next unitResponsive design with media queries
Full text · The whole lesson as one text

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. That makes it the natural tool for photo galleries, card listings, and the overall shape of a page with a header, a sidebar, the main content and a footer.

.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 */
A three-column gallery with one wide item.

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) is a shorter way to write 1fr 1fr 1fr. grid-template-rows does the same for rows, but often you leave rows alone and let them be as tall as their content.

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.

Placing items

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. grid-column: 1 / -1 stretches an item across the whole width, because negative numbers count from the end.

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. Changing the layout for a phone means rewriting only these few strings, often inside a media query.

.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; }
A whole page layout drawn with area names.

Columns that fit the screen

One pattern from MDN's grid guides is worth memorising: grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)). minmax(150px, 1fr) says each column must be at least 150 pixels and may grow to share the space, and auto-fill creates as many columns as fit. The result is a gallery that shows two columns on a phone and five or six on a wide screen, again without a media query. As with flexbox, grid can place items in a different order from the HTML, and the same caution applies: keep the reading order and the visual order the same.

Sketch the grid lines before calculating column widths. For the example with a fixed column and a flexible column, mark the container edges, the fixed track and the gap first. The flexible track receives only the width still available after those parts are removed. Check your result by adding the two tracks and the gap back together. They should reproduce the container width, rather than exceed it.

Key points
  • 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.

Practice questions from Grid basics on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (5)
  • W3C CSS Working Group specifications: CSS Snapshot 2025, Selectors Level 4, CSS Cascading and Inheritance Level 5, CSS Color Level 4, CSS Values and Units Level 4, CSS Box Model Level 3, CSS Box Sizing Level 3, CSS Fonts Level 4, CSS Flexible Box Layout Level 1, CSS Grid Layout Level 2, Media Queries Level 4 and Level 5 (w3.org/TR/, checked 2026-10-17)
  • MDN Web Docs, Learn web development: CSS styling basics and CSS layout, and the CSS reference (developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics, .../Core/CSS_layout, developer.mozilla.org/en-US/docs/Web/CSS, checked 2026-10-17)
  • W3C, CSS history and CSS1 Recommendation of 17 December 1996 (w3.org/Style/CSS20/history.html, w3.org/press-releases/1996/css1-rec, checked 2026-10-17)
  • W3C WCAG 2.2, success criteria 1.4.3 Contrast (Minimum)
  • 1.4.8 Visual Presentation, 1.4.10 Reflow, 1.4.12 Text Spacing (w3.org/TR/WCAG22/, checked 2026-10-17).

Checked: 2026-10-17