Responsive layout
Principles
- Mobile first. Base styles for the narrowest screen; add
@media (min-width: ...)for wider ones. - Content decides breakpoints, not device lists. Add a breakpoint where the layout starts to look wrong.
- Fluid by default. Percentages,
fr,minmax(),clamp()and intrinsic sizing beat fixed widths. - Components respond to their container, not the viewport, when they can appear in different-width slots (container queries).
- Never horizontal scroll at 320 CSS px (WCAG 1.4.10 Reflow), except for content that needs two dimensions (data tables, maps).
Must-haves
<meta name="viewport" content="width=device-width, initial-scale=1">
(never add maximum-scale=1 or user-scalable=no; it blocks zoom)
*, *::before, *::after { box-sizing: border-box; }
img, video, svg { max-width: 100%; height: auto; }
body { margin: 0; }
Patterns
Auto-fitting card grid, no media queries
.cards { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr)); }
min(16rem, 100%) prevents overflow on screens narrower than 16rem.
Sidebar that stacks when there is no room
.with-sidebar { display: flex; flex-wrap: wrap; gap: 1rem; }
.with-sidebar > aside { flex: 1 1 15rem; }
.with-sidebar > main { flex: 999 1 30rem; min-width: 0; }
Fluid type and spacing
:root {
--step-0: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
--step-3: clamp(1.75rem, 1.4rem + 1.75vw, 2.75rem);
--space-m: clamp(1rem, 0.8rem + 1vw, 1.5rem);
}
Keep a rem part in every clamp() so text still scales with browser zoom.
Container queries
.card-slot { container-type: inline-size; }
@container (min-width: 32rem) {
.card { display: grid; grid-template-columns: 10rem 1fr; }
}
Readable line length: max-width: 65ch on text blocks.
Full-height sections on mobile: use min-height: 100dvh (dynamic viewport units) instead of 100vh, which ignores mobile browser toolbars.
Tables: wrap in a container with overflow-x: auto and a visible scroll affordance, or switch to stacked rows on narrow screens.
Debug overflow
- Find the culprit: in DevTools console,
[...document.querySelectorAll('*')].filter(e => e.scrollWidth > document.documentElement.clientWidth). - Usual causes: fixed widths, long unbroken strings (use
overflow-wrap: anywhere), images withoutmax-width, flex children with defaultmin-width: auto(setmin-width: 0), negative margins,100vw(includes the scrollbar).
Test
Check at 320, 375, 768, 1024, 1440 px and at 200% browser zoom, in portrait and landscape, with long content (translations can be 30%+ longer) and with large OS text settings. Touch targets at least 24x24 CSS px (44x44 is a comfortable size).