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.
- ✓grid-template-columns and grid-template-rows define the grid's structure before any content is placed.
- ✓The fr unit distributes available space proportionally between tracks.
- ✓Named grid-template-areas let you describe a layout visually, right inside the CSS.
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.
.gallery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
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.
.layout { display: grid; grid-template-columns: 2fr 1fr; gap: 12px; }
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.
.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; }
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.
.featured { grid-column: span 2; /* takes up two column tracks */ }
auto-fit & minmax()
Combining repeat(auto-fit, minmax(...)) creates a grid that adds or removes columns automatically as the container resizes — no breakpoint required.
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
Choosing the Right Tool
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
Grid containerdisplay: grid turns a parent into a two-dimensional layout container.
fr unitsgrid-template-columns with fr values splits space proportionally between tracks.
Named areasgrid-template-areas visually sketches a layout, matched by grid-area on each child.
Spanninggrid-column: span n lets a single item stretch across multiple tracks.
Auto-responsive gridsrepeat(auto-fit, minmax(...)) adds and removes columns without any media queries.
Try It Yourself
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
Practicing grid structure end to end
Create day25.html linked to a stylesheet and build the following on the page.
- Build a page skeleton with header, sidebar, content, and footer using grid-template-areas.
- Give the sidebar and content columns a 1fr / 3fr ratio.
- Build a photo-style gallery grid with repeat(auto-fit, minmax(140px, 1fr)).
- Make one gallery item span 2 columns with grid-column: span 2.
- Add gap: 12px to both grids and confirm no manual margins are needed.
Recap
display: grid plus grid-template-columns defines a two-dimensional layout structure.
fr distributes available space proportionally across columns or rows.
grid-template-areas sketches a layout visually; grid-area places each child into it.
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.