Sub topic 3
Fluid layouts: let the content flex before you add breakpoints
This chapter assumes you know the box model, CSS selectors, and the units px, rem, and %. It also assumes you have seen CSS Grid and the viewport meta tag from earlier chapters.
Why start with fluid
A breakpoint is a width where the layout changes shape. Many layouts need fewer breakpoints than you expect, because a fluid layout already adapts between them. A fluid layout sizes its parts with relative units and flexible tracks instead of fixed pixel widths.
If you add breakpoints first, you patch problems that flexible sizing would have avoided. If you make the layout fluid first, the breakpoints you add later fix only real problems, such as a line of text becoming too long.
The building blocks
- Percentages and
frunits. Awidth: 50%box follows its parent. A1frgrid track takes a share of the free space. remandchunits.remscales with the root font size.chis about the width of the "0" character, somax-width: 65chlimits a text column to a readable line length.max-widthinstead ofwidth.width: 960pxforces a horizontal scrollbar on a 400px screen.max-width: 60remlets the box shrink and only stops it growing too wide.box-sizing: border-box. Without it,width: 100%plus padding is wider than its parent.
*,
*::before,
*::after {
box-sizing: border-box;
}
img {
max-width: 100%;
height: auto;
}
.page {
max-width: 60rem;
margin-inline: auto;
padding: 1rem;
}
.page p {
max-width: 65ch;
}A grid that reflows itself
The repeat(auto-fit, minmax(...)) pattern makes a grid choose its own column count.
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}Read it as: "make as many columns as fit, each at least 16rem wide, and share any leftover space equally." On a narrow screen you get one column. As the container widens, a second column appears when there is room for two 16rem tracks, then a third. No media query is involved.
The number 16rem is a content decision. It is the narrowest width at which a card still looks good. That is the same question you answer when you choose a breakpoint.
Common errors
- Fixed pixel widths on containers. A
width: 800pxbox overflows any phone. Usemax-widthwith a relative unit. - A minimum larger than the container. If the container is narrower than
16rem,minmax(16rem, 1fr)overflows. Useminmax(min(16rem, 100%), 1fr)to cap the minimum at the container width. - Mixing up
auto-fitandauto-fill. With few items,auto-fillkeeps empty tracks, so the cards stay narrow.auto-fitcollapses the empty tracks, so the cards stretch. - Unbounded stretching. Fluid does not mean unlimited. On a very wide screen, cards and text lines become too wide, so keep a
max-widthon the page wrapper.
What fluid layout cannot do
Fluid sizing scales and reflows. It cannot change the structure. It will not turn a horizontal menu into a hamburger button, or move a sidebar below the main content. Those changes need breakpoints, which the following chapters cover. Fluid layout reduces how many you need.
Exercise
A card grid uses grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)) with gap: 1rem inside a container that is 50rem wide. Assume 1rem = 16px. How many columns appear, and why? What happens if the container shrinks to 12rem?
Show answer
At 50rem, three columns fit: three tracks need 3 × 14rem plus 2 gaps of 1rem = 44rem, which is under 50rem, while four need 4 × 14rem plus 3 gaps = 59rem, which is over. The leftover 6rem is shared among the three tracks through 1fr. At 12rem, the container is narrower than the 14rem minimum, so one column overflows the container. Using minmax(min(14rem, 100%), 1fr) fixes this.