CSS Grid Layout Explained Simply
Flexbox is great for one line of items. Grid is great for rows and columns together, a bit like a table.
Your first grid
fr = a fraction (share) of the free space..grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* 3 equal columns */
gap: 12px;
}fr means "fraction of the free space". 2fr 1fr makes the first column twice as wide as the second.
repeat() and auto-fit
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 16px;
}Read it like this: "make as many columns as fit, each at least 200px wide, and share the extra space". This one line gives you a card grid that fits every screen size without a media query.
repeat(auto-fit, minmax(200px, 1fr)) makes as many columns as fit. No media query needed.Rows: grid-template-rows and auto rows
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: minmax(100px, auto); /* each row at least 100px */
gap: 12px;
}Usually you only set columns, and rows are made automatically as items are added. grid-auto-rows controls the height of those automatic rows. minmax(100px, auto) means "at least 100px, taller if the content needs it".
Make an item bigger
.big {
grid-column: span 2; /* take 2 columns */
}Place items with line numbers
Grid lines are numbered from 1, starting at the left edge. A three-column grid has four lines. You can tell an item to start and end at any line:
.wide {
grid-column: 1 / 3; /* from line 1 to line 3 = two columns */
}
.tall {
grid-column: 3; /* third column */
grid-row: 1 / 3; /* from row line 1 to 3 = two rows */
}
.full {
grid-column: 1 / -1; /* -1 = the last line, so the full width */
}grid-column: 1 / -1 makes an item stretch across the whole grid, whatever the number of columns. Great for a title row inside a card grid.Line up items inside their cells
.grid {
display: grid;
place-items: center; /* center everything in its cell */
}Grid uses the same idea as flexbox: justify-items works left to right and align-items works top to bottom. place-items sets both at once. With one item, display: grid; place-items: center; is the shortest way to center anything in CSS.
A full page layout with grid-template-areas
grid-template-areas you draw the layout with words..page {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-areas:
"header header"
"side main"
"footer footer";
gap: 12px;
}
header { grid-area: header; }
aside { grid-area: side; }
main { grid-area: main; }
footer { grid-area: footer; }You draw the layout with words. It is easy to read and change later.
Change the layout on phones
Grid areas make responsive layouts easy. Write the phone layout first, then redraw it for big screens:
.page {
display: grid;
gap: 12px;
grid-template-areas:
"header"
"main"
"side"
"footer";
}
@media (min-width: 800px) {
.page {
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"side main"
"footer footer";
}
}The HTML stays the same. Only the map changes. On phones everything stacks in one column, with the main content before the sidebar.
Flexbox or Grid?
| Use Flexbox when… | Use Grid when… |
|---|---|
| Items are in one line (row or column) | You need rows and columns together |
| Navbars, buttons, small groups | Page layouts, image galleries, card grids |
Most websites use both. Grid for the big page layout, flexbox for small parts inside.
Mini project: a photo gallery
<div class="gallery">
<img src="images/1.jpg" alt="Beach at sunset" class="big">
<img src="images/2.jpg" alt="Coconut trees">
<img src="images/3.jpg" alt="Fishing boats">
<img src="images/4.jpg" alt="Sea shells on sand">
<img src="images/5.jpg" alt="Lighthouse">
</div>.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
grid-auto-rows: 160px;
gap: 8px;
}
.gallery img {
width: 100%;
height: 100%;
object-fit: cover; /* crop to fill the cell */
border-radius: 8px;
}
.gallery .big {
grid-column: span 2;
grid-row: span 2;
}Try making the browser narrow and wide. The gallery fills each row by itself, and the big photo always takes two columns and two rows.
Common mistakes
| Problem | Fix |
|---|---|
| Items do not form columns | Put display: grid on the parent and set grid-template-columns |
| Grid is too wide on phones | Use minmax(0, 1fr) or auto-fit instead of fixed pixel columns |
| Area names do nothing | Each item needs grid-area: name, and every row in the map needs the same number of names |
span 2 makes a gap | Add grid-auto-flow: dense to let smaller items fill holes |
Cheat sheet
| Property | What it does |
|---|---|
display: grid | Turn on grid |
grid-template-columns | Set the columns |
repeat(auto-fit, minmax(200px, 1fr)) | Responsive columns |
gap | Space between cells |
grid-column: span 2 | Take two columns |
grid-column: 1 / -1 | Full width |
grid-template-areas | Draw the layout with names |
place-items: center | Center inside cells |
Practice time
- Build a photo gallery with
repeat(auto-fit, minmax(180px, 1fr)). - Build a page with header, sidebar, main and footer using grid areas.
- Make one gallery photo span two columns.
- Make a grid where the first item always spans the full width.
- Change your page layout from one column on phones to two columns above 800px.
Quick summary
display: grid+grid-template-columnsmakes columns.frshares space;repeat(auto-fit, minmax())makes responsive grids.- Grid for big layout, flexbox for small parts.