Turning on Grid
The fr unit means "a fraction of the available space." Three equal 1fr columns split the width evenly โ no percentage math, no guessing.
Unequal columns, on purpose
This is the exact pattern behind a classic sidebar layout: a fixed 200px column that never changes size, next to a content column that fills whatever space is left.
Rows work the same way
auto means "just as tall as the content needs." This three-row pattern โ header sized to its content, content filling remaining space, footer sized to its content โ is how you pin a footer to the bottom of the screen even on short pages.
grid-template-areas: naming your layout
This is Grid's most readable feature โ you can literally draw your layout as text:
Each word in grid-template-areas is a named region. Assign any element to a region with grid-area, and it snaps into place โ no counting columns or calculating positions.
Grid vs. Flexbox: which one to reach for
| Flexbox | Grid | |
|---|---|---|
| Dimensions | One direction at a time (row OR column) | Two directions at once (rows AND columns) |
| Best for | Nav bars, button groups, centering content | Full page layouts, image galleries, dashboards |
| Common combo | Grid for the overall page, Flexbox inside individual sections โ extremely common together | |
Responsive grid without media queries
This single line creates a grid that automatically fits as many 200px-minimum columns as will comfortably fit, and reflows them as the screen resizes โ no @media breakpoints needed for a huge number of layouts.
Try it yourself
grid-template-areas: a header, a 200px sidebar, a main content area, and a footer. Confirm it holds together as you resize your browser window.
Level up: a responsive gallery
.card divs inside a container using grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)) and gap: 20px. Resize the window and watch the number of columns adjust automatically โ no breakpoints written by you at all.
1. What does the fr unit represent in CSS Grid?
2. Which is better suited for a full page layout with a sidebar, header, and footer?
3. What does repeat(auto-fit, minmax(200px, 1fr)) do?
grid-template-areas makes complex layouts genuinely readable, and auto-fit/minmax gives you responsive grids without writing a single media query.