Short, practical guidance on fluid typography using modern CSS — focusing on concise principles and minimal, real code examples using clamp(), viewport units, and container queries. References link to authoritative specs and articles; snippets are ready‑to‑use. (Smashing Magazine)
Instead of fixed font sizes or hardcoded breakpoints, fluid typography lets text scale smoothly based on the viewport or container size — improving readability and reducing maintenance. Modern CSS eliminates many media queries. (Smashing Magazine)
clamp(min, preferred, max) sets a font size that:
- never goes below the minimum
- scales fluidly via a viewport‑based preferred value
- never exceeds the maximum This allows smooth scaling between small and large screens without breakpoint logic. (MDN Web Docs)
h1 {
font-size: clamp(1.5rem, 5vw, 3rem);
}
p {
font-size: clamp(1rem, 2.5vw, 1.25rem);
}1.5rem/1rem: lower bound for accessibility/readability5vw/2.5vw: 5% / 2.5% of viewport width3rem/1.25rem: upper bound so text doesn’t grow too large
:root {
--base-min: 1rem;
--base-max: 1.25rem;
}
body {
font-size: clamp(var(--base-min), 2vw, var(--base-max));
}Using CSS variables simplifies consistent type scaling across components. (Smashing Magazine)
Viewport units allow scaling based on window size. Best practice is to combine viewport units with fixed units inside clamp() so text stays readable on extremes.
Example:
h2 {
font-size: clamp(1.25rem, 4vw, 2rem);
}- Without clamp, using just
4vwmight produce overly large or tiny text at extremes. (Smashing Magazine)
Container queries let typography adapt based on container size, not just viewport — great for modular components. (Wikipedia)
.card {
container-type: inline-size;
}@container (min-width: 30rem) {
.card-title {
font-size: 2rem;
}
}
@container (max-width: 29.99rem) {
.card-title {
font-size: 1.5rem;
}
}When the .card grows/shrinks, title size adapts based on its space — useful in components that float, stack, or shift layouts.
More advanced patterns combine clamp() with container queries:
@container (min-width: 30rem) {
.card-title {
font-size: clamp(1.5rem, 3vw, 2.25rem);
}
}Keep type accessible & readable
- Start with a meaningful minimum value (
1remor larger). (web.dev) - Limit the scaling range (e.g., 1–1.5×) so text remains legible.
- Use relative units (
rem,em) for min/max to respect user preferences and zoom settings. (web.dev)
Minimize breakpoints
- Prefer fluid scaling (with
clamp()and viewport units) over many media queries. (Smashing Magazine)
Combine with semantic structure
- Use fluid type on headings, body text, and components without repetitive breakpoints.
- Optionally layer container queries for elements in variable layout contexts. (Wikipedia)
- MDN
clamp()reference: specs and examples of min/max/viewport math. (MDN Web Docs) - Smashing Magazine: Fluid typography principles and parameter tips. (Smashing Magazine)
- Container Queries (CSS Conditional Rules Level 5): component‑scoped responsive rules. (Wikipedia)