Clamp Fluid Typography Responsive
Create fluid typography that scales between minimum and maximum sizes using the CSS clamp function with viewport units.
Code
h1 {
font-size: clamp(1.5rem, 4vw + 1rem, 3rem);
line-height: 1.2;
}
h2 {
font-size: clamp(1.25rem, 3vw + 0.75rem, 2.25rem);
line-height: 1.3;
}
p {
font-size: clamp(1rem, 1.5vw + 0.5rem, 1.125rem);
line-height: 1.6;
}
.container {
max-width: clamp(320px, 90vw, 1200px);
margin: 0 auto;
}What the code does
clamp() evaluates to [min, preferred, max] — the value scales fluidly but never leaves the bounds.
The rules target h1 — each selector names the element it restyles, in source order.
Interactive Tools for This Topic
CSS clamp() GeneratorGenerate responsive clamp(MIN, PREFERRED, MAX) values with automatic calc() wrapping for expressions.Aspect Ratio CalculatorSimplify a width-to-height ratio, show its decimal value, and derive a matching height for a new width.Line Height CalculatorConvert between a unitless line-height ratio and its pixel value for a given font size.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- Which elements does this stylesheet target?
- The rules bind to `h1`; when two rules tie in specificity, the one written later in the file wins.
Report a problem with this exampleOpens a pre-filled issue on GitHub.