Lesson 18 of 23

In progress

Course progress

HTML and CSS

CSS Grid for page layout

Build two-dimensional layouts with template columns, fr units and auto-fit, and place items without a pile of wrappers.

Grid is for layouts where rows and columns both matter: a page with a header, sidebar and content area, a gallery, a pricing table. It lets you describe the structure once and place items into it.

What you will learn

  • Define columns and rows with fr, repeat and minmax.
  • Place items by line numbers or named areas.
  • Build a responsive gallery that reflows without media queries.

Columns and rows

HTML
Editable. Runs in your browser.





Press Run to see the output.

fr means a fraction of the free space. The second column is twice the first. gap spaces both rows and columns; row-gap and column-gap set them separately.

repeat(3, 1fr) is shorthand for three equal columns. grid-template-rows does the same vertically, though rows often size themselves from content.

A real page shell

HTML
Editable. Runs in your browser.





Press Run to see the output.

grid-template-areas draws the layout as text. The names must form rectangles, and each area maps to an item with grid-area. Reading the CSS tells you the page structure without running it, which is the main advantage over line numbers.

On a narrow screen, redefine the areas to a single column:

@media (max-width: 40rem) {
.page {
grid-template-columns: 1fr;
grid-template-areas: "header" "main" "sidebar" "footer";
}
}

The HTML does not change. That is the payoff of naming areas.

Placing by line

Grid lines are numbered from 1 at the start edge. grid-column: 1 / 3 spans from line 1 to line 3, covering two columns. grid-row works the same way.

HTML
Editable. Runs in your browser.





Press Run to see the output.

Rows created by content rather than by grid-template-rows are implicit. grid-auto-rows: minmax(120px, auto) gives them a sensible minimum without fixing their height, which keeps a gallery even when captions differ in length.

place-items is shorthand for align-items and justify-items, and place-content does the same for distribution. For a centred empty state, display: grid; place-items: center; min-height: 40vh is shorter than the Flexbox equivalent and behaves the same. Both shorthands accept two values when the axes need to differ from each other.

Items placed automatically fill the next free cell. An item with an explicit span takes its space first, and the rest flow around it. grid-column: span 2 is a relative version that does not depend on the starting line.

Responsive without media queries

HTML
Editable. Runs in your browser.





Press Run to see the output.

auto-fit creates as many columns as fit at the minimum width, and 1fr lets them share the remaining space. auto-fill keeps empty tracks instead of stretching items, which matters when you want consistent column widths.

minmax(200px, 1fr) means "at least 200 pixels, at most an equal share". This one line replaces a stack of breakpoints for a card grid.

Grid or Flexbox?

Grid handles the page skeleton and any layout where alignment across rows matters. Flexbox handles a row of buttons or a card’s internal stack. Using both is normal.

A grid item’s default minimum size is its content, so a long word or a wide table can blow out a column. minmax(0, 1fr) allows the track to shrink below the content width, which is the fix for most grid overflow.

A fixed grid-template-columns: 300px 300px 300px overflows a 700-pixel container and forces horizontal scrolling. Use repeat(3, minmax(0, 1fr)) or auto-fit so the columns can share the available width.

Build a gallery with auto-fit columns at a 180-pixel minimum, six items, and a gap of 12 pixels. Then make the first item span two columns on wide screens using a media query.

Show solution
HTML
Editable. Runs in your browser.





Press Run to see the output.

The base grid reflows on its own. The media query adds the feature only when there is room, and span 2 does not depend on a starting line.

Recap

  • fr shares free space; repeat and minmax keep templates short.
  • grid-template-areas describes a page shell and can be redefined at a breakpoint.
  • auto-fit with a minimum gives responsive cards without media queries.

Next, you will add breakpoints and preference queries deliberately, instead of chasing device widths.

Can I nest grids?
Yes, a grid item can itself be a grid container. That is how a page shell contains a card grid. Keep nesting shallow enough to debug.
Why is my grid overflowing horizontally?
Usually a track cannot shrink below its content, or an item has a fixed width. Try `minmax(0, 1fr)` on the track and check for long unbroken text.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.