Nishaglobal Education logo

CSS Grid: rows and columns together

Build two-dimensional layouts with CSS Grid: columns with fr units and repeat(), gaps, items that span columns, auto-fitting card grids and whole-page layouts with named areas.

Lesson 11 of 1340 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Create grid columns with fr units, repeat() and gap
  • Make items span several columns or rows
  • Build a responsive card grid and a page layout with grid-template-areas
Outline
  1. What you'll learn and why it matters
  2. Your first grid
  3. Lines and spanning
  4. A responsive card grid in one line
  5. Page layouts with named areas
  6. Practice
  7. Common mistakes
  8. Recap
  9. Quiz
  10. Mark lesson complete

What you'll learn and why it matters

Flexbox lines things up in one direction. CSS Grid controls rows and columns at the same time, like a spreadsheet. It is perfect for photo galleries, product grids, dashboards and the overall layout of a page.

Your first grid

Put display: grid on the container and say how many columns you want with grid-template-columns. The fr unit means *a fraction of the free space*.

CSS
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 12px;
}

.tile {
  background-color: #ede9fe;
  border: 2px solid #7c3aed;
  padding: 1rem;
  text-align: center;
}
Show the HTML this CSS styles
<div class="grid">
  <div class="tile">A</div>
  <div class="tile">B</div>
  <div class="tile">C</div>
  <div class="tile">D</div>
  <div class="tile">E</div>
</div>

Browser preview

Three equal columns. The five items fill the grid in order, left to right, and a new row starts automatically when a row is full.

Columns valueResult
1fr 1fr 1frThree equal columns
repeat(3, 1fr)The same, written shorter
2fr 1frThe first column is twice as wide as the second
200px 1frA fixed 200px column and one that takes the rest

Lines and spanning

Grid lines are numbered from 1, starting at the left edge. An item can stretch across several columns with grid-column.

1234123Agrid-column: 1 / 3BCDENumbers are grid lines. A starts at line 1 and endsat line 3, so it covers two columns.grid-template-columns: 1fr 1fr 1fr;gap: 12px;
A three-column grid with lines numbered 1 to 4 across the top and 1 to 3 down the side. Item A has grid-column: 1 / 3, so it starts at line 1, ends at line 3 and covers two columns. B, C, D and E fill the remaining cells.
CSS
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.tile {
  background-color: #ede9fe;
  border: 2px solid #7c3aed;
  padding: 1rem;
  text-align: center;
}

.wide {
  grid-column: 1 / 3;
}
Show the HTML this CSS styles
<div class="grid">
  <div class="tile wide">A</div>
  <div class="tile">B</div>
  <div class="tile">C</div>
  <div class="tile">D</div>
  <div class="tile">E</div>
</div>

Browser preview

grid-column: 1 / 3 means *start at line 1, end at line 3*. You can also write grid-column: span 2 to cover two columns wherever the item lands. grid-row works the same way for rows.

A responsive card grid in one line

This line is one of the most useful in all of CSS:

CSS
.products {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
  gap: 1rem;
}

.product {
  padding: 1rem;
  border: 2px solid #0f766e;
  border-radius: 0.75rem;
  background-color: #f0fdfa;
}
Show the HTML this CSS styles
<div class="products">
  <div class="product">Notebook</div>
  <div class="product">Pen set</div>
  <div class="product">Backpack</div>
  <div class="product">Water bottle</div>
</div>

Browser preview

PartMeaning
minmax(12rem, 1fr)Each column is at least 12rem wide, and can grow to share the space
auto-fitFit as many columns as the width allows

Switch the preview between Laptop (1024px) and Phone (360px): on a wide screen you get four columns, on a phone just one, and sizes in between get two or three. No media queries needed.

Page layouts with named areas

grid-template-areas lets you draw the layout with words. Each quoted string is one row; repeating a name makes that area span columns.

CSS
.page {
  display: grid;
  grid-template-columns: 1fr 3fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  gap: 8px;
}

.page > * {
  padding: 1rem;
  background-color: #f1f5f9;
  border: 1px solid #94a3b8;
}

.page header { grid-area: header; }
.page aside { grid-area: sidebar; }
.page main { grid-area: main; }
.page footer { grid-area: footer; }
Show the HTML this CSS styles
<div class="page">
  <header>Header</header>
  <aside>Sidebar</aside>
  <main>Main content</main>
  <footer>Footer</footer>
</div>

Browser preview

.page > * uses the child combinator >: it selects every direct child of .page. In the next lesson you will change this layout to a single column on phones with a media query.

Practice

Guided exercise

A photo gallery

Show the six gallery items in a grid with 3 equal columns and a 10px gap. Make the first item span 2 columns and 2 rows.

  1. On .gallery set display: grid, grid-template-columns: repeat(3, 1fr) and gap: 10px.
  2. Give .featured grid-column: span 2 and grid-row: span 2.
Show the solution
Solution
.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.gallery div {
  min-height: 5rem;
  padding: 0.5rem;
  background-color: #fee2e2;
  border: 2px solid #dc2626;
}

.featured {
  grid-column: span 2;
  grid-row: span 2;
}
Show the HTML this CSS styles
<div class="gallery">
  <div class="featured">1 (featured)</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
</div>

Browser preview

How to get there: span 2 stretches an item across two tracks wherever it is placed. The featured item takes a 2 by 2 block, and the grid flows the other items around it.

Your turn

Sidebar layout

Make a two-column layout: a sidebar that is always 8rem wide and a main area that takes the rest, with a 1.5rem gap.

Show the solution
Solution
.layout {
  display: grid;
  grid-template-columns: 8rem 1fr;
  gap: 1.5rem;
}

.layout > * {
  padding: 1rem;
  background-color: #ecfeff;
  border: 1px solid #0891b2;
}
Show the HTML this CSS styles
<div class="layout">
  <aside>Filters</aside>
  <main>Search results</main>
</div>

Browser preview

How to get there: A fixed size and an fr column work together: the browser gives the sidebar 8rem first, then hands all the remaining space to the 1fr column.

Common mistakes

Setting grid-template-columns on the items

You see: Nothing changes

Fix: Grid properties for columns and gaps go on the container.

Counting cells instead of lines

You see: grid-column: 1 / 2 covers only one column

Fix: Lines are the edges between cells: to cover columns 1 and 2, end at line 3.

Fixed columns like repeat(4, 250px)

You see: Sideways scrolling on phones

Fix: Use fr units or repeat(auto-fit, minmax(…, 1fr)).

Misspelt or missing area names

You see: Items fall into the wrong place or into extra rows

Fix: Each grid-area name must match a name in grid-template-areas exactly.

Recap

  • display: grid and grid-template-columns create columns; fr shares the free space.
  • Lines are numbered from 1; grid-column: 1 / 3 or span 2 stretches an item.
  • repeat(auto-fit, minmax(12rem, 1fr)) makes a responsive card grid.
  • grid-template-areas draws a page layout with names; use Flexbox for one-direction lines.

Key terms

fr
A grid unit meaning one fraction of the free space.
Grid line
A numbered dividing line between grid columns or rows.
Track
A single column or row in a grid.

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. What does grid-template-columns: 2fr 1fr create?

  2. 2. An item should cover the first two columns. Which value is right?

  3. 3. What does repeat(auto-fit, minmax(12rem, 1fr)) do?

  4. 4. Which job suits Flexbox better than Grid?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.