▥ CSS Intermediate

CSS Grid

⏱ 2 hr3 topicsLive playground
🎯 By the end: You can create a grid with columns and gaps, use fr units and repeat(), and build a responsive grid that reflows on its own.

Flexbox arranges things in one direction — a row or a column. CSS Grid works in two directions at once: rows and columns together. It's how you build real page layouts — galleries, dashboards, card grids — with remarkably little code. Flexbox and Grid are partners, not rivals: reach for Grid when you're laying out in two dimensions.

1Columns, gaps and the fr unit

Turn an element into a grid, then describe its columns with grid-template-columns:

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;  /* three equal columns */
  gap: 12px;
}

The star here is the fr unit — a "fraction" of the free space. 1fr 1fr 1fr means three equal columns; 2fr 1fr means the first column is twice as wide as the second. No percentages, no maths.

And repeat() saves typing: grid-template-columns: repeat(3, 1fr) is the same as 1fr 1fr 1fr. The gap property adds even gutters between every cell.

A three-column grid
HTML
CSS
Live preview
What's happening: repeat(3, 1fr) makes three equal columns; the six items flow into two rows automatically. Try changing it to repeat(2, 1fr) for two columns, or '2fr 1fr 1fr' to make the first column wider.
Key points
  • display: grid + grid-template-columns defines the columns.
  • The fr unit splits free space into fractions — 1fr 1fr = two equal columns.
  • repeat(3, 1fr) is shorthand; gap adds even gutters between cells.

2Spanning cells and responsive grids

An item can span several columns with grid-column:

.feature {
  grid-column: span 2;  /* take up two columns */
}

A grid that responds on its own

Here's a small piece of magic. This one line builds a grid that automatically fits as many columns as will comfortably fit, then wraps — no media queries needed:

grid-template-columns:
  repeat(auto-fit, minmax(200px, 1fr));

It reads: "make as many columns as fit, each at least 200px wide, sharing space equally." On a wide screen you get four columns; on a phone, one — automatically. It's a favourite for card galleries.

Grid for the overall page skeleton and big layouts; Flexbox for aligning items within a component. Used together they cover virtually every layout you'll ever need.
A self-responsive card grid
HTML
CSS
Live preview
What's happening: repeat(auto-fit, minmax(140px, 1fr)) fits as many 140px+ columns as it can and wraps the rest — a fully responsive grid with no media queries. Narrow the preview and watch the cards reflow.
Key points
  • grid-column: span 2 makes an item stretch across multiple columns.
  • repeat(auto-fit, minmax(200px, 1fr)) builds a responsive grid with no media queries.
  • Use Grid for 2-D page layouts and Flexbox for aligning within components — together they cover everything.

3Play with the grid — live

Drag the sliders below to change the number of columns, the item count and the gap, and watch the grid rebuild instantly. Notice how items flow into the columns and wrap onto new rows on their own.

Grid visualizer — drag the sliders
Key points
  • More columns means items share the row; extra items wrap to new rows automatically.
  • gap adds even spacing between every cell.
  • The CSS readout is exactly what produces the grid you see.

★ Practical: a responsive gallery

In any playground:

  1. Make a grid of six boxes with repeat(3, 1fr) and a gap.
  2. Make one box span two columns with grid-column: span 2.
  3. Switch the columns to repeat(auto-fit, minmax(150px, 1fr)).
  4. Narrow the preview and confirm the grid reflows on its own.

Ready to test yourself?

Take the short quiz. Score 60% or more to mark this lesson complete.

Start the quiz →

💡 Log in to save your progress and earn the certificate.