Chapter 8 of 15

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

A three column, two row grid with 1fr labels and the gap marked
A grid has columns and rows. Items fill the cells in order.
Three columns: the first twice as wide as the others
fr = a fraction (share) of the free space.
CSS
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;  /* 3 equal columns */
  gap: 12px;
}
Result in the browser
1
2
3
4
5
6

fr means "fraction of the free space". 2fr 1fr makes the first column twice as wide as the second.

repeat() and auto-fit

CSS
.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.

The same grid shown on a laptop with 4 columns, a tablet with 2 and a phone with 1
repeat(auto-fit, minmax(200px, 1fr)) makes as many columns as fit. No media query needed.
Result in the browser
1
2
3
4
5

Rows: grid-template-rows and auto rows

CSS
.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

CSS
.big {
  grid-column: span 2;   /* take 2 columns */
}
Result in the browser
1 (span 2)
2
3
4
5

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:

CSS
.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 */
}
Result in the browser
1 / 3
tall
3
4
1 / -1
Most useful trickgrid-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

CSS
.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

Page layout with header, side, main and footer next to the matching grid-template-areas code
With grid-template-areas you draw the layout with words.
CSS
.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; }
Result in the browser
header
side
main
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:

CSS
.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 groupsPage layouts, image galleries, card grids

Most websites use both. Grid for the big page layout, flexbox for small parts inside.

Left: flexbox row with one arrow. Right: grid with arrows in both directions
Flexbox lines things up in one row (or column). Grid controls rows and columns together.

Mini project: a photo gallery

HTML
<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>
CSS
.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;
}
Result in the browser

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

ProblemFix
Items do not form columnsPut display: grid on the parent and set grid-template-columns
Grid is too wide on phonesUse minmax(0, 1fr) or auto-fit instead of fixed pixel columns
Area names do nothingEach item needs grid-area: name, and every row in the map needs the same number of names
span 2 makes a gapAdd grid-auto-flow: dense to let smaller items fill holes

Cheat sheet

PropertyWhat it does
display: gridTurn on grid
grid-template-columnsSet the columns
repeat(auto-fit, minmax(200px, 1fr))Responsive columns
gapSpace between cells
grid-column: span 2Take two columns
grid-column: 1 / -1Full width
grid-template-areasDraw the layout with names
place-items: centerCenter inside cells

Practice time

  1. Build a photo gallery with repeat(auto-fit, minmax(180px, 1fr)).
  2. Build a page with header, sidebar, main and footer using grid areas.
  3. Make one gallery photo span two columns.
  4. Make a grid where the first item always spans the full width.
  5. Change your page layout from one column on phones to two columns above 800px.

Quick summary

  • display: grid + grid-template-columns makes columns.
  • fr shares space; repeat(auto-fit, minmax()) makes responsive grids.
  • Grid for big layout, flexbox for small parts.