Skip to content
Codewilds

Lesson 22 of 29 · Grid & Responsive Design

CSS Grid Tutorial: Columns, fr Units and Gap

medium20 XP3 min read

Flexbox lines items up in one direction: a row or a column. CSS Grid works in two directions at once, rows and columns, so items line up neatly both ways. It's the natural choice for galleries, dashboards and whole page layouts.

Making a grid

Write display: grid on the parent and describe its columns with grid-template-columns. The children are placed into the cells automatically, from left to right and top to bottom, and new rows are added as needed:

.gallery {
  display: grid;
  grid-template-columns: 200px 200px 200px; /* three columns, 200px each */
}

With six children, this grid has two rows of three. You don't describe the rows: they appear automatically, each one as tall as its tallest item.

The fr unit

Fixed pixel columns don't adapt to the screen. The fr unit (a fraction of the free space) shares out the available width instead:

.layout {
  display: grid;
  grid-template-columns: 2fr 1fr; /* the first column is twice as wide */
}

1fr 1fr 1fr gives three equal columns. You can mix units too: 250px 1fr makes a fixed 250px sidebar and a main column that takes the rest.

repeat() and gap

Typing 1fr again and again gets tedious, so repeat() does it for you: repeat(4, 1fr) means exactly the same as 1fr 1fr 1fr 1fr.

gap sets the space between the rows and the columns, just like in flexbox. Use row-gap and column-gap when the two should be different:

.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

The fr columns are calculated after the gaps are taken out, so the grid always fits its container exactly.

Spanning several columns

An item can stretch across several cells. grid-column: span 2 makes it cover two columns, and grid-row: span 2 makes it two rows tall:

.featured {
  grid-column: span 2;
}

You can also use the numbered lines between the columns: grid-column: 1 / 3 runs from line 1 to line 3 (the first two columns), and grid-column: 1 / -1 stretches across the full width, however many columns there are.

Grid or flexbox?

Use flexbox for a single row or column of things whose size comes from their content, like a navbar or a row of buttons. Use grid when items should line up in rows and columns, like cards in a gallery. Most real pages use both: a grid for the overall layout, and flexbox inside the cards.

Common mistakes

  • Putting grid-template-columns on the items. It belongs on the grid container, next to display: grid.
  • Forgetting the comma in repeat(). repeat(3 1fr) is invalid, and the browser silently ignores the whole declaration.
  • Confusing grid-column: 2 with grid-column: span 2. The first places the item in column 2; the second makes it cover two columns.

Assignment

Turn the realm dashboard into a grid:

  1. Make .dashboard a grid (display: grid) with 4 equal columns that share the full width: grid-template-columns: repeat(4, 1fr).
  2. Put a 16px gap between the rows and the columns.
  3. Make the .featured tile span 2 columns (grid-column: span 2).

The featured tile and two small tiles then fill the first row, and the other four tiles fill the second row.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.

Lesson last updated on .

Write code ↓
HTML & CSS+20 XP

Lesson 22 is part of the full course. The first 7 lessons are free.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.