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.
🎯 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
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*.
.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 value | Result |
|---|---|
1fr 1fr 1fr | Three equal columns |
repeat(3, 1fr) | The same, written shorter |
2fr 1fr | The first column is twice as wide as the second |
200px 1fr | A 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.
.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:
.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
| Part | Meaning |
|---|---|
minmax(12rem, 1fr) | Each column is at least 12rem wide, and can grow to share the space |
auto-fit | Fit 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.
.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.
- On
.gallerysetdisplay: grid,grid-template-columns: repeat(3, 1fr)andgap: 10px. - Give
.featuredgrid-column: span 2andgrid-row: span 2.
Show the 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
.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: gridandgrid-template-columnscreate columns;frshares the free space.- Lines are numbered from 1;
grid-column: 1 / 3orspan 2stretches an item. repeat(auto-fit, minmax(12rem, 1fr))makes a responsive card grid.grid-template-areasdraws 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. What does grid-template-columns: 2fr 1fr create?
2. An item should cover the first two columns. Which value is right?
3. What does repeat(auto-fit, minmax(12rem, 1fr)) do?
4. Which job suits Flexbox better than Grid?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.