A responsive layout adapts to the space it's given. There are two ways to get there: intrinsic layout, where the layout algorithms respond to available space by themselves, and conditional rules (media and container queries) that switch styles at a threshold. Lead with the first.
Intrinsic techniques:
- Flex wrapping.
flex-wrap: wrapplus aflex-basisputs items side by side when they fit and stacks them when they don't. - Grid auto-repeat.
repeat(auto-fit, minmax(min(12rem, 100%), 1fr))makes as many columns as fit. Themin()stops the minimum overflowing a container narrower than12rem. - Fluid values.
width: min(100%, 70ch)caps line length;clamp()scales type and spacing between bounds. - No fixed widths on boxes that hold content.
Breakpoints are for structural changes no algorithm can infer: navigation collapsing into a menu button, a table turning into cards. Rules for them:
- Content decides, not devices. Widen the window until the design looks wrong; that's the breakpoint. Device widths change every year.
- Mobile-first. The base styles are the narrow layout and
@media (width >= 48em)adds to them, so wide screens get extra rules rather than undoing them. The narrow layout matters on desktop too: WCAG 1.4.10 Reflow wants content usable at 320 CSS px wide, which is a 1280 px window at 400% zoom. embreakpoints. In a media query,emis relative to the initialfont-size, the user's default, so someone with larger text gets the narrower layout sooner. The page'shtml { font-size }doesn't affect it.- Components use container queries. A card in a sidebar and in the main column sees the same viewport but different space.