HTML & CSS · 14-day planAll days
Build two-dimensional layouts with CSS Grid. Define columns and rows with fr units and repeat, make an image gallery that adapts to any screen with auto-fit and minmax, place items across several cells, and lay out a whole page with named grid areas.
By the end of today you can
- Turn a container into a grid and define columns with grid-template-columns
- Use the fr unit, repeat() and gap
- Make responsive galleries with repeat(auto-fit, minmax())
- Span items across columns and rows
- Lay out a page with grid-template-areas, and choose between Grid and Flexbox
Flexbox lines things up in one direction. CSS Grid works in two: rows and columns at once. It's perfect for galleries, dashboards and whole-page layouts.
Columns and the fr unit
display: gridon the container; children become grid items and fill the cells in order.grid-template-columnssets the column sizes.fr= a fraction of the free space:1fr 2frgives the second column twice as much.repeat(3, 1fr)=1fr 1fr 1fr.- Rows are created automatically as needed;
grid-template-rowssizes them explicitly.
A responsive gallery in one line
Use the Phone / Tablet / Desktop buttons above the preview: the number of columns changes by itself.
repeat(auto-fit, minmax(160px, 1fr)) means "as many columns as fit, each at least 160px, stretched to fill the row". No media queries needed. object-fit: cover fills each box with the image, cropping instead of squashing.
Spanning cells
grid-column: span 2: two columns wide.grid-row: span 2: two rows tall.grid-column: 1 / -1: from the first line to the last, the full width.
Whole-page layouts with areas
grid-template-areas draws the layout as a little text map; each element joins its area with grid-area. Try the Phone width: the sidebar is cramped. On Day 12 a media query will stack it on small screens by just redrawing the map.
Grid or Flexbox?
| Use Flexbox for | Use Grid for |
|---|---|
| One row or column | Rows and columns |
| Navbars, toolbars, button groups | Galleries, card grids, dashboards |
| Content decides the size | The layout decides the size |
| Centring something | Whole-page layouts |
They work well together: Grid for the page, Flexbox inside a card or the header.
Recap
display: grid+grid-template-columns;frshares space;repeat()saves typing;gapspaces.repeat(auto-fit, minmax(200px, 1fr)): responsive columns without media queries.grid-column: span 2,grid-row: span 2,1 / -1for full width.grid-template-areas+grid-areafor page layouts.- Flexbox for one direction; Grid for two.
Tomorrow: responsive design, so everything works on every screen.
Knowledge check
Test what you learned
5 questions · pick an answer to see if you're right
- 1
What does grid-template-columns: 1fr 2fr; do?
- 2
What does repeat(3, 1fr) mean?
- 3
Why is repeat(auto-fit, minmax(200px, 1fr)) so popular?
- 4
How do you make a grid item span two columns?
- 5
When is Flexbox usually a better fit than Grid?
Finished day 11?
Mark complete and continue to Day 12: Responsive DesignWas this lesson helpful?
Need a breather? Take a two-minute break