Sub topic 9
Responsive layouts with grid and flexbox
This chapter assumes you know basic CSS Grid and Flexbox syntax, @media rules with min-width, and the mobile-first approach from earlier chapters. Here you combine them: the layout system decides how items are arranged, and media queries decide when the arrangement changes.
Division of labor
Media queries should change as little as possible. Grid and flexbox can already adapt to available space, so you only add a query when the number of columns or the direction of flow must change at a specific width.
| Tool | Best for | What the query changes |
|---|---|---|
| Grid | Two-dimensional layouts (rows and columns) | `grid-template-columns` |
| Flexbox | One-dimensional flow (a row or a column) | `flex-direction`, `flex-wrap`, `flex-basis` |
A mobile-first grid
Start with one column, then add columns as the viewport grows. The fr unit shares the free space equally, and gap adds spacing without margin hacks.
.cards {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
}
@media (min-width: 40em) {
.cards {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 64em) {
.cards {
grid-template-columns: repeat(3, 1fr);
}
}Each query overrides only the one property that changes. Everything else is inherited from the base rule.
A flexbox navigation
Flexbox suits a navigation bar. On narrow screens, stack the links in a column. When there is room, put them in a row.
.nav {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
@media (min-width: 40em) {
.nav {
flex-direction: row;
justify-content: space-between;
}
}Layouts that need fewer queries
Sometimes you can skip the query entirely. This grid creates as many 15rem-minimum columns as fit and stretches them to fill the row.
.gallery {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
}Flexbox can do something similar with wrapping:
.tags {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.tags > * {
flex: 1 1 10rem;
}Use explicit breakpoints when the design calls for a specific column count, such as exactly three columns on wide screens. Use auto-fit when "as many as fit" is acceptable.
Common errors
- Using
auto-fillwhen you meantauto-fit. Withauto-fill, empty tracks keep their space. Withauto-fit, empty tracks collapse, so the existing items stretch. - Overflow from a large
minmax()minimum. If the minimum is wider than the container, the grid overflows on small screens. Useminmax(min(15rem, 100%), 1fr)to cap it. - Writing desktop rules first. Using
max-widthqueries to undo a multi-column base layout leads to more overrides than a mobile-first approach. - Putting
display: gridinside a query. Declare the layout type in the base rule. Change only the values that differ.
Exercise
A .gallery should show one column on small screens and exactly four columns at 60em and wider. Write the CSS in mobile-first form.
Show answer
.gallery { display: grid; gap: 1rem; grid-template-columns: 1fr; } @media (min-width: 60em) { .gallery { grid-template-columns: repeat(4, 1fr); } } The base rule sets the single column, and the min-width query overrides only the column definition.