Step-by-step
-
1
Turn Any Element into a Grid Container
Set
display: gridon the parent. Nothing moves yet — all children still stack vertically, because you have not defined any columns. This is your starting point.css.container { display: grid; } -
2
Define Columns with grid-template-columns
This is where Grid earns its reputation. The declaration below creates as many columns as will fit, each at least 260px wide, sharing leftover space equally. Add or remove cards — the grid reflows automatically, zero media queries.
css.container { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); } -
3
Add Spacing with gap
gapis the modern shorthand forrow-gapandcolumn-gap. It sets gutters between tracks — not on the outer edges. No more negative margins or:last-childhacks.css.container { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 1.5rem; } -
4
Name Layout Areas with grid-template-areas
For page-level layouts (header, sidebar, main content, footer), named areas make the structure self-documenting. The visual ASCII diagram in your CSS is exactly how the layout renders.
css.page { display: grid; grid-template-columns: 240px 1fr; grid-template-rows: auto 1fr auto; grid-template-areas: "header header" "sidebar main" "footer footer"; min-height: 100dvh; gap: 0; } header { grid-area: header; } aside { grid-area: sidebar; } main { grid-area: main; } footer { grid-area: footer; } -
5
Span Items Across Multiple Columns
A featured card or a banner that should occupy more horizontal space can span multiple columns with a single declaration. Use
-1as the end line to stretch all the way to the last column, regardless of how many columns the grid has.css/* span exactly 2 columns */ .featured { grid-column: span 2; } /* stretch from first to last column */ .banner { grid-column: 1 / -1; } -
6
Add Breakpoints Only Where Truly Needed
auto-fit+minmaxhandles most responsive needs automatically. Only add a media query when the layout itself needs to change structurally — for example, collapsing the sidebar on small screens.css@media (max-width: 640px) { .page { grid-template-columns: 1fr; grid-template-areas: "header" "main" "sidebar" "footer"; } } -
7
Debug Grid with Chrome DevTools
Open DevTools, click the Elements tab, and find your grid container. A small grid badge appears next to it. Click the badge to overlay the grid lines directly on the page — column numbers, row numbers, area names, and gap sizes all rendered visually. This is the fastest way to diagnose misaligned items.
Tips & gotchas
- <code>auto-fit</code> collapses empty tracks; <code>auto-fill</code> keeps them as ghost columns. Use <code>auto-fit</code> for card grids so items stretch to fill the row.
- Avoid fixed row heights (<code>grid-template-rows: 200px 200px</code>) unless you need a masonry-like effect — let content drive row height instead.
- Grid and Flexbox are complementary, not competing. Use Grid for two-dimensional page structure; use Flexbox for one-dimensional component internals (navigation items, button groups).
- The <code>fr</code> unit distributes <em>available</em> space after fixed-size and <code>min-content</code> tracks have been resolved. It will never make a column smaller than its content.
Wrapping up
CSS Grid has transformed layout from a constant struggle into a straightforward craft. Master auto-fit + minmax for card grids, grid-template-areas for page structure, and grid-column: span N for featured items. Together, these three patterns cover the vast majority of real-world layout requirements.