Fluid typography with clamp()
Scale font size smoothly between a minimum and maximum as the viewport grows, with no media queries.
- CSS
- GreenSnippets
- MIT
:root {
--step-0: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
--step-1: clamp(1.25rem, 1.1rem + 0.75vw, 1.75rem);
--step-2: clamp(1.5rem, 1.2rem + 1.5vw, 2.5rem);
}
body { font-size: var(--step-0); line-height: 1.6; }
h2 { font-size: var(--step-1); }
h1 { font-size: var(--step-2); line-height: 1.15; }Example / expected output
/* h1 grows from 24px on phones to 40px on wide screens. */How it works
clamp(min, preferred, max) keeps the value within bounds. The preferred value mixes a rem base with a viewport unit, so text grows with the screen but never gets tiny or huge. Including a rem part keeps the text zoomable for users who change their browser font size.
This snippet has been reviewed but was not executed by an automated test. Test it in your own environment before relying on it.
Related snippets
- CSS
Card that adapts to its container with container queries
Switch a card from stacked to side-by-side based on the space it has, not the viewport.
- CSS
CSS-only scroll-snap carousel
A horizontally scrolling gallery that snaps to each card, with no JavaScript.
- CSS
Respect prefers-reduced-motion
Turn off or shorten animations for users who ask their system for less motion.
- CSS
A small modern CSS reset
Sensible defaults for box sizing, media, form fonts and text wrapping in a dozen lines.

