Responsive Design: Fluid CSS and Breakpoints
Responsive design is not a list of device widths; it is a set of decisions about how content reflows when space runs out. Done well, the same document serves a 360-pixel phone, a 768-pixel tablet, a 27-inch monitor and a split-window browser without a single dedicated mobile page. This lesson assembles the modern toolkit â mobile-first cascade order, content-driven breakpoints, clamp() fluid type, container queries for components and the new viewport units â into a system you can apply to any layout, on any project, without returning to breakpoint roulette.
Mobile-first is a cascade strategy, not a slogan
Mobile-first means the base stylesheet describes the narrowest experience, and each media query adds capacity as space grows. The reason is cascade efficiency: a min-width query only ever adds rules, so a phone downloads and applies the smallest possible rule set, while a desktop applies the base rules and then upgrades them. The inverted approach â desktop styles with max-width overrides â forces phones to parse every override and makes it easy to forget one, leaving a stray wide layout on a small screen.
:root {
--space-xs: 0.5rem;
--space-sm: 1rem;
--space-md: 1.5rem;
--space-lg: 2.5rem;
--measure: 65ch;
}
.container {
inline-size: 100%;
max-inline-size: 72rem;
margin-inline: auto;
padding-inline: var(--space-sm);
}
.stack > * + * { margin-block-start: var(--space-sm); }
.card-grid {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: var(--space-md);
}
.site-header {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: var(--space-sm);
padding-block: var(--space-xs);
}
.site-nav { display: none; width: 100%; }
.site-nav[data-open="true"] { display: block; }
.site-nav ul { display: flex; flex-direction: column; gap: var(--space-xs); list-style: none; padding: 0; }
@media (min-width: 40rem) {
.card-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.site-nav { display: block; width: auto; margin-inline-start: auto; }
.site-nav ul { flex-direction: row; gap: var(--space-md); }
.menu-toggle { display: none; }
}
@media (min-width: 64rem) {
.card-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.container { padding-inline: var(--space-md); }
}
@media (min-width: 80rem) {
.card-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
Notice there are no device names in the comments or variables. The 40, 64 and 80 rem thresholds exist because the content â nav items, card columns, measure of text â starts to look wrong below them, not because a particular tablet is 768 pixels wide.
Choosing breakpoints from content
The reliable way to find a breakpoint is to open the page, resize slowly, and note the width where something breaks: text lines get so long the eye loses the next line, cards become too narrow for their images, the navigation wraps into an awkward second row. Those failure points become your media query thresholds. Device databases suggest starting values, but your content decides the final numbers.
Resist the urge to add many breakpoints. Three or four well-chosen thresholds cover almost every marketing page or dashboard; each additional one multiplies the combinations you must test. When two components fail at different widths, prefer fixing the component with a container query instead of adding a page-level breakpoint.
Fluid type with clamp()
Fixed font sizes in media queries make text jump between discrete sizes. clamp(min, preferred, max) interpolates continuously: the preferred value usually tracks viewport width, while the min and max keep the type readable at the extremes. A tiny set of fluid steps replaces a table of per-breakpoint sizes.
:root {
--step--1: clamp(0.83rem, 0.78rem + 0.25vw, 0.94rem);
--step-0: clamp(1rem, 0.95rem + 0.25vw, 1.13rem);
--step-1: clamp(1.2rem, 1.1rem + 0.5vw, 1.44rem);
--step-2: clamp(1.44rem, 1.28rem + 0.8vw, 1.87rem);
--step-3: clamp(1.73rem, 1.46rem + 1.25vw, 2.5rem);
--step-4: clamp(2.07rem, 1.6rem + 2vw, 3.35rem);
}
body {
font-size: var(--step-0);
line-height: 1.6;
}
h1 { font-size: var(--step-4); text-wrap: balance; }
h2 { font-size: var(--step-3); }
h3 { font-size: var(--step-2); }
.prose p { max-inline-size: var(--measure); }
.hero {
min-block-size: 70vh;
min-block-size: 70dvh;
display: grid;
place-items: center;
padding-block: var(--space-lg);
}
.media-frame {
aspect-ratio: 16 / 9;
inline-size: 100%;
background: #e2e8f0;
}
Each step grows smoothly between the clamps, so there is no perceptible size jump at any particular width. Pair fluid type with a stable measure â around 60 to 70 characters per line â and the reading experience stays consistent across devices even as the font size drifts.
Container queries for component reuse
Media queries answer "how wide is the viewport"; container queries answer "how wide is this component's context." A card that lives in a sidebar and in a main column is two designs, and a viewport query cannot distinguish them. Mark a sizing wrapper, then write queries against it:
.product-card-container {
container-type: inline-size;
container-name: product-card;
}
.product-card {
display: grid;
gap: 0.75rem;
padding: 1rem;
border: 1px solid #e2e8f0;
border-radius: 12px;
background: #ffffff;
}
@container product-card (min-width: 24rem) {
.product-card {
grid-template-columns: 8.5rem minmax(0, 1fr);
align-items: center;
}
}
@container product-card (min-width: 34rem) {
.product-card {
grid-template-columns: 12rem minmax(0, 1fr);
padding: 1.25rem 1.5rem;
}
.product-card .summary {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
}
@container product-card (max-width: 20rem) {
.product-card .badge { display: none; }
}
Ship the narrow design as the base rule and add the wider layouts in container queries â the same mobile-first discipline, scoped to the component. One caution: containers establish an independent formatting context, so put container-type on a wrapper when the component itself needs to participate in the parent's grid or flex layout.
Viewport units beyond vh and vw
The classic viewport units measure the layout viewport, which on mobile browsers excludes or includes the dynamic URL bar inconsistently â a hero sized to 100vh can hide its button behind browser chrome or leave an awkward gap when the bar collapses. The dynamic viewport unit dvh tracks the visible area live, while svh tracks the smallest state. Use dvh for full-screen sections and svh when you want to avoid content shifting as the bar moves.
| Unit | Measures | Typical use |
|---|---|---|
| vh / vw | Initial layout viewport | Legacy full-height sections |
| dvh | Current visible viewport, dynamic | Heroes, modals, on-screen stages |
| svh | Smallest visible viewport | Stable sizing that never resizes mid-scroll |
| lvh | Largest visible viewport | Rare; background art |
| rem | Root font size | Type and spacing scales |
| cqi / cqw | Container query units | Padding inside container queries |
| min() / max() / clamp() | Composed constraints | Guards like min(100%, 20rem) |
Container query units (cqi, cqw) pair naturally with container queries: internal padding expressed in cqi scales with the component rather than the screen, which keeps small cards compact without a second breakpoint.
Testing across sizes efficiently
Testing does not require a device lab. Start with the responsive mode in dev tools, but verify on at least one real phone because touch scrolling, safe areas and keyboard behaviour differ from emulation. Walk each breakpoint in both directions â narrow to wide and wide to narrow â since collapsed navigation and reflowed grids often fail in only one direction. Check zoom to 200 percent, which exposes fixed heights and clipped text that normal widths hide. And test with the OS text-size preference raised, because users who enlarge system text expect the layout to accommodate them.
Record the breakpoints and the reasoning in the stylesheet comments â "40rem: nav fits on one row" tells the next developer when it is safe to change a threshold. Responsive work accumulates institutional knowledge faster than any framework migration, and a documented content-driven breakpoint set is the kind of asset that keeps paying off years later.
One underrated testing trick is resizing with dev tools open while watching the layout's reflow points in the elements panel. When an element suddenly gains a horizontal scrollbar or a nav item wraps, note the exact width, round to a sensible rem value, and record why. Over a few months the team builds a shared map of its own content failures, and debates about where to add a breakpoint resolve themselves by looking at the map instead of relitigating from scratch. Keep that map next to the stylesheet or in the design system docs so new pages inherit the same discipline.
Common Mistakes
Naming breakpoints after devices (tablet, iPad, desktop) in comments and variables
Fix: Name them after the content failure they fix â nav-row, card-pair, wide-measure â so future edits stay intentional.
Using 100vh for a full-screen hero on mobile browsers
Fix: Switch to 100dvh, with svh when you must avoid resizing as the browser bar moves.
Adding a viewport media query for a component that also appears in sidebars
Fix: Convert the component to container queries and keep viewport breakpoints for page scaffolding only.
Setting font sizes at five discrete breakpoints, producing jarring jumps
Fix: Replace the ladder with clamp() steps so type interpolates smoothly between sensible bounds.
Testing only at the breakpoint widths, never between them
Fix: Sample midpoints and odd window sizes; most overflow bugs live between the declared thresholds.
Forgetting min-width: 0 on grid and flex children, causing horizontal scroll
Fix: Use minmax(0, 1fr) tracks or min-inline-size: 0 on children that contain wide content.
Key Takeaways
- Mobile-first ordering keeps the base rule set minimal and lets each min-width query add capacity rather than override it.
- Derive breakpoints from content failures you can observe, keep the count small, and document the reason beside each one.
- clamp() replaces fontSize ladders with smooth fluid steps bounded by readable minimums and maximums.
- Container queries make components responsive to their context; reserve viewport media queries for page scaffolding.
- Prefer dvh for full-screen sections and pair container query units with cqi-based internal spacing.
- Verify on a real device, at 200 percent zoom, and with enlarged system text â emulation alone hides the hardest cases.