Sub topic 7
Fluid layouts with Flexbox and Grid
This chapter assumes you know basic Flexbox and Grid syntax, the box model, and how to write a min-width media query. Here you will use those tools so a layout adapts to the space it has, with few media queries.
Fluid versus adaptive
A fixed layout uses pixel widths. An adaptive layout switches between a few fixed designs at breakpoints. A fluid layout lets the browser compute sizes from the available space, so it works at widths you never tested. Media queries then handle only the points where the structure must change, such as going from one column to two.
Grid that adapts without a media query
The most useful fluid pattern is a grid that decides its own column count:
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
}Read it from the inside out:
minmax(16rem, 1fr)means each column is at least 16rem wide and may grow to share the leftover space equally.repeat(auto-fit, ...)creates as many such columns as fit in the container.min(100%, 16rem)caps the minimum at the container width. Without it, a container narrower than 16rem would overflow.
On a 320px screen you get one column. On a wide screen you get three or four. No breakpoint is involved.
Grid with a media query
Some layouts have a deliberate structure, such as a main column and a sidebar. For these, set the base styles for small screens and add the columns when there is room:
/* Base: small screens */
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
/* Wider screens: add, don't undo */
@media (min-width: 48em) {
.layout {
grid-template-columns: 2fr 1fr;
padding: 2rem;
}
}The base rule has no grid-template-columns, so the children stack in one column. The query adds columns and does not need to undo anything. The fr unit divides free space in proportion, so the columns stay fluid even above the breakpoint.
Flexbox for rows that wrap
Flexbox suits one-dimensional content such as navigation links or tag lists. Allow items to wrap and give them a preferred size:
.tags {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.tags > li {
flex: 1 1 8rem;
}flex: 1 1 8rem means grow, shrink, and start from a basis of 8rem. Items wrap to a new line when they can't keep 8rem. Unlike Grid, the last row's items stretch to fill the row, so they may be wider than the items above.
Choosing between them
| Situation | Better fit |
|---|---|
| Cards that should line up in columns across rows | Grid |
| Page regions such as main and sidebar | Grid |
| A row of buttons or links that wraps | Flexbox |
| Items that should keep their natural width | Flexbox |
Common errors
- Horizontal scrolling on small screens. A fixed
minmaxminimum wider than the container causes it. Wrap the minimum inmin(100%, ...). - Grid items overflowing. Grid and flex items default to
min-width: auto, so long words or wide children can't shrink. Addmin-width: 0to the item, or useoverflow-wrap: anywherefor long text. - Using
auto-filland expecting stretch.auto-fillkeeps empty tracks, so a few items don't expand to fill the row.auto-fitcollapses empty tracks and lets items grow. - Undoing styles in queries. If a min-width query has to reset many base properties, the base styles probably include desktop assumptions.
Exercise
A card grid uses grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)). On a 280px-wide phone, the page scrolls sideways. What is the cause, and how do you fix it?
Show answer
The container, after padding, is narrower than the 16rem (256px) minimum plus its surrounding space, or the minimum exceeds the container width, so the single column cannot shrink and overflows. Change the minimum to min(100%, 16rem) so it never exceeds the container, giving repeat(auto-fit, minmax(min(100%, 16rem), 1fr)).
Exercise
You want a sidebar layout that is one column on phones and two columns (2fr and 1fr) from 48em upward. Which styles go in the base rule and which go in the media query?
Show answer
The base rule holds display: grid, gap and padding and has no column template, so the items stack. The @media (min-width: 48em) query adds grid-template-columns: 2fr 1fr and any larger padding.