MODULE 04 · LAYOUT & RESPONSIVENESS

CSS Grid Layout

Flexbox handles a single row or column beautifully. Grid does something flexbox can't — it lays elements out across rows and columns at the same time, making true page-level layouts possible with a handful of properties.

Day 25 of 30 Intermediate ~23 min

Two Dimensions, Not One

display: grid turns an element into a grid container. Unlike flexbox's single axis, grid works across rows and columns simultaneously — you define the structure once on the parent, and every child slots into it.

display: grid & grid-template-columns

grid-template-columns defines how many columns exist and how wide each one is. The fr unit ("fraction") splits remaining space proportionally — 1fr 1fr 1fr creates three equal columns.

style.css
.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
3 EQUAL COLUMNS — repeat(3, 1fr)
1
2
3
4
5
6

Proportional, Not Just Equal

fr values don't have to match. grid-template-columns: 2fr 1fr gives the first column twice the space of the second — perfect for a content-plus-sidebar layout without a single percentage calculation.

style.css
.layout {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 12px;
}
2fr 1fr — CONTENT TWICE AS WIDE AS SIDEBAR
content — 2fr
sidebar — 1fr

Sketch the Layout Right in the CSS

grid-template-areas lets you name regions and literally draw the layout as a grid of strings — each child is placed with grid-area matching one of those names.

style.css
.page {
  display: grid;
  grid-template-columns: 1fr 3fr;
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
  gap: 10px;
}

.site-header { grid-area: header; }
.sidebar     { grid-area: sidebar; }
.content     { grid-area: content; }
.site-footer { grid-area: footer; }
A FULL PAGE LAYOUT FROM NAMED AREAS
header
sidebar
content
GOOD TO KNOW

The quoted rows in grid-template-areas must line up into a rectangle — every row needs the same number of names, and a repeated name spans across those cells automatically.

grid-column & grid-row

Without named areas, an item can span multiple columns or rows directly with grid-column: span 2 or a specific start/end line.

style.css
.featured {
  grid-column: span 2;   /* takes up two column tracks */
}
ONE ITEM SPANNING TWO COLUMNS IN A 4-COLUMN GRID
span 2
3
4
5
6

auto-fit & minmax()

Combining repeat(auto-fit, minmax(...)) creates a grid that adds or removes columns automatically as the container resizes — no breakpoint required.

style.css
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}
RESIZE THE WINDOW — COLUMNS WRAP AUTOMATICALLY
1
2
3
4

Choosing the Right Tool

WHEN TO REACH FOR EACH
USE FLEXBOX FOR A single row or column — nav bars, button groups, or centering one element inside another.
USE GRID FOR Full page layouts, photo galleries, or anything that needs to align across both rows and columns at once.

Most real interfaces use both — Grid for the overall page skeleton, Flexbox for arranging content inside individual grid cells.

Breaking Down What You Just Learned

1

Grid containerdisplay: grid turns a parent into a two-dimensional layout container.

2

fr unitsgrid-template-columns with fr values splits space proportionally between tracks.

3

Named areasgrid-template-areas visually sketches a layout, matched by grid-area on each child.

4

Spanninggrid-column: span n lets a single item stretch across multiple tracks.

5

Auto-responsive gridsrepeat(auto-fit, minmax(...)) adds and removes columns without any media queries.

Try It Yourself

EXERCISE

Take the header/sidebar/content/footer shape from Day 15's project and rebuild the whole page layout using grid-template-areas instead of manual positioning.

Build a Grid-Based Page Skeleton

HANDS-ON EXERCISE

Practicing grid structure end to end

Create day25.html linked to a stylesheet and build the following on the page.

  1. Build a page skeleton with header, sidebar, content, and footer using grid-template-areas.
  2. Give the sidebar and content columns a 1fr / 3fr ratio.
  3. Build a photo-style gallery grid with repeat(auto-fit, minmax(140px, 1fr)).
  4. Make one gallery item span 2 columns with grid-column: span 2.
  5. Add gap: 12px to both grids and confirm no manual margins are needed.

Recap

GRID BASICS

display: grid plus grid-template-columns defines a two-dimensional layout structure.

FR UNITS

fr distributes available space proportionally across columns or rows.

NAMED AREAS

grid-template-areas sketches a layout visually; grid-area places each child into it.

RESPONSIVE GRIDS

auto-fit with minmax() adds and drops columns automatically as space changes.

Key Takeaways

  • display: grid turns a parent into a grid container that lays children out in rows and columns at once.
  • fr units divide remaining space proportionally — 1fr 1fr 1fr splits evenly, 2fr 1fr splits 2:1.
  • grid-template-areas lets you name and visually sketch a layout directly inside the stylesheet.
  • grid-column: span n and grid-row: span n stretch an item across multiple tracks.
  • repeat(auto-fit, minmax(min, 1fr)) builds a responsive grid without writing a single media query.
Course Overview