IntermediateLesson 14 of 305 min read

CSS Grid basics: columns, rows, fr units and spanning

Lay out pages in two dimensions with CSS Grid: columns, the fr unit, repeat, gaps and spanning cells.

In this lesson you will

  • Create a grid with columns and gaps
  • Use the fr unit and repeat()
  • Span items across cells
  • Choose between grid and flexbox

About 16 minutes

Rows and columns at the same time

Flexbox arranges things in one line at a time. But look at a newspaper front page, a photo gallery or a dashboard. Items line up in both rows and columns, with some stretching across several cells. That is a grid, and CSS has a tool built for it: CSS Grid. If flexbox is a row of chairs, grid is the seating plan of a whole theatre.

Switching it on

.layout {
  display: grid;
  grid-template-columns: 200px 1fr 1fr;
  gap: 16px;
}

As with flexbox, you switch grid on with display: grid on the parent, and the direct children become grid items. The next property defines the columns: here, one fixed 200-pixel column and two flexible ones.

The fr unit

fr stands for fraction of the free space. In 1fr 1fr 1fr the three columns share the space equally. In 1fr 2fr the second column is twice the size of the first. You can mix fr with fixed sizes: 200px 1fr gives a sidebar of exactly 200 pixels and a main column that takes all the rest.

Create a three-column grid with equal columns using 1fr each, and a 12px gap.

<style>
 .grid { /* grid here */ font-family: Arial; }
 .grid div { background: #EFEAFD; padding: 18px; border-radius: 10px; text-align: center; }
</style>
<div class="grid"><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div></div>

display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px;

<style>
 .grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; font-family: Arial; }
 .grid div { background: #EFEAFD; padding: 18px; border-radius: 10px; text-align: center; }
</style>
<div class="grid"><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div></div>

The repeat function

grid-template-columns: repeat(3, 1fr);        /* three equal columns */
grid-template-columns: repeat(4, 100px);      /* four 100px columns */
grid-template-columns: 200px repeat(2, 1fr);  /* mix them */

Rows

You usually let rows create themselves. Items fill the grid row by row, and new rows are added as needed (the implicit grid). To control their height, use grid-auto-rows or define them with grid-template-rows.

.grid { grid-auto-rows: 120px; }
.grid { grid-auto-rows: minmax(100px, auto); }   /* at least 100px, grow with content */

Spanning cells

An item can stretch across several columns or rows. The simplest way is the span keyword.

.feature { grid-column: span 2; }   /* two columns wide */
.tall    { grid-row: span 2; }      /* two rows tall */
.hero    { grid-column: 1 / -1; }   /* from the first line to the last: full width */

The second form uses grid lines, the numbered dividing lines between tracks. A grid with three columns has four vertical lines, numbered 1 to 4 from the left, and also numbered backwards as -1 to -4 from the right. So 1 / -1 always means “all the way across”, whatever the number of columns.

Make the first item span the full width of the three-column grid, and the last item span two columns.

<style>
 .grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; font-family: Arial; }
 .grid div { background: #EFEAFD; padding: 16px; border-radius: 10px; text-align: center; }
 .first { }
 .last { }
</style>
<div class="grid"><div class="first">Full width</div><div>2</div><div>3</div><div>4</div><div class="last">Spans two</div></div>

.first { grid-column: 1 / -1; } .last { grid-column: span 2; }

<style>
 .grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; font-family: Arial; }
 .grid div { background: #EFEAFD; padding: 16px; border-radius: 10px; text-align: center; }
 .first { grid-column: 1 / -1; }
 .last { grid-column: span 2; }
</style>
<div class="grid"><div class="first">Full width</div><div>2</div><div>3</div><div>4</div><div class="last">Spans two</div></div>

Gap, alignment and justification

  • gap, row-gap and column-gap space the tracks apart.
  • justify-items aligns items horizontally inside their cells, and align-items vertically. The default is stretch.
  • justify-content and align-content position the whole grid inside its container when the tracks are smaller than the container.
  • place-items: center centres items in their cells both ways with one line.

Two common grid layouts

/* A blog layout: sidebar + content */
.page { display: grid; grid-template-columns: 260px 1fr; gap: 32px; }

/* A photo gallery */
.gallery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.gallery img { width: 100%; aspect-ratio: 1; object-fit: cover; }

The gallery pictures use aspect-ratio: 1 to become perfect squares and object-fit: cover to crop without squashing. The combination gives a tidy grid out of photos of any size.

Grid or flexbox?

  • Use flexbox when you are arranging items in a single line and the content should decide the sizes: menus, button groups, toolbars.
  • Use grid when you plan the page in rows and columns first and place content into it: page layouts, card galleries, dashboards.
  • They combine beautifully. A grid defines the page, and flexbox arranges the contents of each cell.
Good to know

Grid is supported in every current browser. You do not need fallbacks unless you must support extremely old software.

Common mistake

Counting grid lines wrong. Remember: a grid with N columns has N + 1 column lines. grid-column: 2 / 4 means from line 2 to line 4, which spans two columns, not three.

Use the grid inspector

In Chrome and Firefox developer tools, click the small grid badge next to display: grid in the Elements panel. The browser draws the lines and numbers on your page. It is the fastest way to understand a grid.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What does fr stand for?

  1. Frame rate
  2. Fraction of the free space
  3. Fixed ratio
  4. Flex row

fr divides the free space between tracks.

Which declares three equal columns?

  1. grid-template-columns: repeat(3, 1fr)
  2. columns: 3
  3. grid: 3
  4. display: columns

repeat(3, 1fr) gives three equal flexible tracks.

What does grid-column: 1 / -1 do?

  1. Hides the item
  2. Spans the full width
  3. Reverses columns
  4. Adds a gap

From the first line to the last line.

When is grid better than flexbox?

  1. For a single row of buttons
  2. For layouts in rows and columns together
  3. For text colour
  4. For fonts

Grid is two-dimensional.

How many column lines does a 3-column grid have?

  1. 2
  2. 3
  3. 4
  4. 6

N columns have N + 1 lines.

Summary

  • display: grid plus grid-template-columns defines the columns.
  • fr shares free space; repeat() saves typing.
  • grid-column spans cells; 1 / -1 means full width.
  • Grid is for two dimensions, flexbox for one.

Next lesson

Next: the powerful grid patterns used on real sites, such as auto-fit and named areas.

Questions

Can I mix pixels and fr?

Yes. 200px 1fr gives a fixed sidebar and a flexible main column.

Do I need to define rows?

Usually not. Rows are created automatically. Use grid-auto-rows to control their height.

Is grid harder than flexbox?

It has more features, but the basics are quick to learn, and the browser tools show the lines.

Progress is saved in this browser.