CSS Grid: tracks, areas and implicit grids
Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.
What you will be able to do
#- Define a grid with columns, rows and gaps.
- Place items with grid lines or named areas.
Overview
#Grid arranges children in two dimensions at once. You define tracks with grid-template-columns and grid-template-rows, and items flow into the cells automatically. fr units divide the free space proportionally, which makes fluid layouts simple.
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
Items flow row by row by default. You can place an item explicitly by line number with grid-column and grid-row, or by name. Named areas make complex layouts readable:
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"sidebar main";
gap: 1.5rem;
}
.page > .sidebar { grid-area: sidebar; }
.page > .main { grid-area: main; }
When you place items outside the defined tracks, Grid creates implicit tracks to hold them. Implicit tracks are sized by grid-auto-rows and grid-auto-columns, and their default auto size can produce uneven rows. Setting grid-auto-rows: 1fr or a fixed size keeps implicit rows consistent.
Grid and Flexbox are complementary. Use Grid for the two-dimensional structure of a page or a card layout, and Flexbox for one-dimensional alignment inside a component. The responsive card grid is a classic case: grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)) creates as many columns as fit, each at least 240px, without a media query.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Create a responsive card grid that fits as many columns as possible, each at least 240px wide, with a 1rem gap.
starter.css.cards { display: grid; /* add tracks and gap */ }Reveal the reference answer
reference.css.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; }Authored expected output: A grid that adds or removes columns as space allows, with every column at least 240px.
auto-fit with minmax creates as many tracks as fit and lets them grow, so no media query is needed.
Exercise 2Implement
Place a header across two columns using named grid areas.
starter.css.layout { display: grid; grid-template-columns: 1fr 1fr; } .header { /* place across both columns */ }Reveal the reference answer
reference.css.layout { display: grid; grid-template-columns: 1fr 1fr; grid-template-areas: "header header"; } .header { grid-area: header; }Authored expected output: The header spans both columns via a named area.
Repeating the area name across columns makes the item span those tracks.
Common misconceptions
#- Grid replaces Flexbox entirely.
- Implicit grid tracks are always the same size as explicit ones.
Follow-up practice
#- html-css-20-positioning-stacking
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.