Sub topic 3
Fluid layouts: let the content flex before you add breakpoints
This chapter assumes you know the CSS box model, selectors, and the units px, rem, and %. It also assumes you have seen CSS Grid and Flexbox at a basic level. By the end, you will know how far a layout can flex without a single media query, and how to recognize the point where a breakpoint is the right tool.
Why flex first
A breakpoint is a width at which the layout rule set changes. Each one adds code to maintain and a width at which something can go wrong. A fluid layout reduces how many you need.
A fluid layout is one where sizes are defined relative to the space available instead of in fixed pixels. The browser recalculates them as the container grows or shrinks. A layout built this way already works at 340px, 517px, and 1013px, not only at the widths you tested.
The working rule is: make the layout flex, find where it still breaks, and add a breakpoint only at that point.
Relative units and what they depend on
Different units depend on different things. Choosing the wrong one is the most common cause of a layout that does not flex.
| Unit | Relative to | Typical use |
|---|---|---|
| `%` | The parent's size (width for most properties) | Widths of block children |
| `rem` | The root font size, usually 16px | Spacing, type, max widths |
| `em` | The current element's font size | Padding that scales with a component's text |
| `vw` | 1% of the viewport width | Fluid type, full-bleed sections |
| `fr` | A share of leftover space in a grid | Grid track sizes |
| `ch` | The width of the "0" glyph | Line length of text |
Two points matter in practice.
- Percentages resolve against the parent. If the parent has no defined width, the result may not be what you expect. Vertical percentages (such as
height: 50%) need the parent to have an explicit height, so avoid them for layout. remfollows the user's browser font setting. Amax-width: 40remcontainer grows when a user enlarges their default text, which keeps line length comfortable. Amax-width: 640pxcontainer does not.
Intrinsic sizing: let content set the size
Between "fixed" and "percentage" there is a third option: sizes that come from the content itself. These are intrinsic sizes.
min-contentis the narrowest the box can get without overflowing, such as the longest unbreakable word.max-contentis the width the box would take if it never wrapped.fit-contentis the content's natural width, capped at the available space.
Grid and Flexbox use these ideas internally. You rarely type them, but they explain why a flex item with a long URL refuses to shrink. A flex item has min-width: auto, which prevents it from going below its min-content size. The fix is min-width: 0 on that item, plus overflow-wrap: anywhere on the text.
Building a fluid page shell
A good starting point is a container that takes the available width but stops growing at a comfortable maximum.
.page {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}min() picks the smaller of its arguments. On a narrow screen, 100% - 2rem is smaller, so the page fills the width minus a 1rem gutter on each side. On a wide screen, 70rem is smaller, so the page stops growing and centers itself. One declaration replaces a max-width, side padding, and a media query.
The related functions are max(), which picks the larger value, and clamp(min, preferred, max), which keeps a value between two limits. The type chapter uses clamp() heavily. Here, it is enough to know that all three accept mixed units, so calc-style arithmetic such as 100% - 2rem is allowed inside them.
Grids that choose their own column count
The most useful fluid technique is a grid that decides how many columns fit. This example comes from the Topic Spine:
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}Read it from the inside out.
minmax(16rem, 1fr)means each column is at least16remwide and can grow to an equal share of the free space.repeat(auto-fit, ...)creates as many such columns as fit in the container.- Any leftover space is shared by the columns that exist, because of
1fr.
At 20rem of container width you get one column. At 34rem you get two (2 × 16rem plus the gap). At 52rem you get three. No media query decides this. The count changes at widths the content defines.
auto-fit versus auto-fill
Both keywords create as many tracks as fit. They differ when there are fewer items than tracks.
auto-fillkeeps the empty tracks. Three cards in a space for five leave two blank columns, so the cards stay at their normal size.auto-fitcollapses empty tracks to zero, so the three cards stretch to fill the row.
Use auto-fit when you want items to fill the row. Use auto-fill when you want a stable column size regardless of item count, for example a gallery where a lone item should not become huge.
A trap in the minimum
minmax(16rem, 1fr) has a flaw on very narrow containers. If the container is narrower than 16rem, such as a 280px phone minus padding, the column cannot shrink below 16rem and the grid overflows horizontally. Guard against it with min():
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr));
}min(16rem, 100%) caps the minimum at the container width, so a single column never exceeds its container.
Flexbox wrapping
Flexbox can do something similar with wrapping. Each item states a preferred size and the line breaks when items no longer fit.
.tags {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.tags > * {
flex: 1 1 10rem;
}flex: 1 1 10rem means grow with factor 1, shrink with factor 1, and start from a basis of 10rem. Items wrap when a row cannot hold another 10rem item.
Flexbox and Grid differ in one way that matters here. Flex wraps each line independently, so a last row with one item stretches wide while the rows above have three. Grid keeps every row aligned to the same columns. For card layouts, Grid usually gives the more predictable result. For a row of tags or buttons, wrapping Flexbox is fine.
Media that scales
Images and video are common sources of horizontal overflow. This rule prevents most of it:
img,
video {
max-width: 100%;
height: auto;
}max-width: 100% stops the media from exceeding its container. height: auto keeps the aspect ratio when the width is reduced. Add width and height attributes in the HTML as well, so the browser can reserve space before the file loads and avoid layout shift.
Where fluid layout stops working
Fluid techniques adjust sizes and column counts. They do not change the structure of the page. A breakpoint becomes the right tool when:
- A sidebar has to move from beside the content to below it, or the reverse.
- The order or grouping of regions has to change, such as a three-column page that needs a 2fr / 1fr split at tablet width.
- Navigation has to switch from a menu button to a visible link row.
- A component's content becomes unreadable at a width the fluid rules do not address, for example a table with six columns at 400px.
A useful test is to drag the browser from 320px to 1600px with only your fluid rules applied. Note each width at which something looks wrong, such as lines that are too long, gaps that are too large, or controls that are cramped. Those widths are your candidate breakpoints, and the next chapters cover how to choose and record them.
Common errors
- Fixed widths on inner elements. A
width: 600pxcard inside a fluid column overflows at 400px. Usemax-widthinstead ofwidthso the element can shrink. - Mixing
100vwwith a scrollbar.100vwincludes the vertical scrollbar width on some platforms, which causes a horizontal scrollbar. Prefer100%for layout widths. - No maximum width. Fluid does not mean unlimited. At 2000px, an unconstrained text column has lines over 200 characters.
- Fractional rounding.
1frcolumns can produce subpixel widths. This is usually invisible, but borders on adjacent items can look uneven on some displays. - Treating
auto-fitas a breakpoint replacement for everything. It handles column count. It does not reorder content or change navigation.
Exercise
A card grid uses grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr)). The container is 40rem wide and the gap is 1rem. How many columns appear, and what happens if the container shrinks to 15rem?
Show answer
At 40rem, two columns fit, since 2 × 18rem + 1rem = 37rem, and three would need 56rem. Each column grows to about 19.5rem through the 1fr share. At 15rem, the column minimum of 18rem is larger than the container, so the grid overflows horizontally. Wrapping the minimum in min(18rem, 100%) fixes it by capping the column at the container width.
Exercise
You build a page with fluid rules only and drag the window from 320px to 1600px. Everything flexes well except that at about 900px, a sidebar and the main content are both too narrow to read. Should you fix this with another fluid rule or with a breakpoint, and why?
Show answer
Use a breakpoint. The problem is structural: two regions compete for width. A fluid rule can only resize them. A min-width media query near 900px can stack the sidebar below the content for narrower widths and place it beside the content above that width, so each region gets enough room.