Developers & Technical · Guide · Developer Utilities
How to use CSS clamp()
clamp() syntax, the preferred-value math, fluid typography without media queries, container query units, accessibility, browser support.
clamp() is one of the most useful additions to CSS in the last decade. It lets you set a value that fluidly scales between a minimum and maximum, based on viewport width or any other context. It replaces entire stacks of media queries with a single line. This guide covers the clamp() syntax, the math behind fluid typography, how to derive preferred-value expressions, when to reach for clamp() vs media queries, browser support, and common pitfalls.
Advertisement
Syntax
clamp(MIN, PREFERRED, MAX)
Returns the preferred value, bounded by min and max.
Simple example:
font-size: clamp(1rem, 2.5vw, 1.5rem);
At viewport width 400px → 2.5vw = 10px → clamped up to 16px (1rem).
At 800px → 20px → within range → returns 20px.
At 1200px → 30px → clamped down to 24px (1.5rem).
Fluid typography — the killer use case
The old way: media-query breakpoints.
h1 { font-size: 2rem; }
@media (min-width: 640px) { h1 { font-size: 2.5rem; }}
@media (min-width: 1024px) { h1 { font-size: 3rem; }}The clamp() way:
h1 { font-size: clamp(2rem, 1.5rem + 2vw, 3rem); }Size grows smoothly with viewport. No jarring jumps at breakpoints. Less CSS.
The preferred-value formula
The middle argument has to do some math: combine a viewport unit with a fixed offset so the value hits your min at a specific small viewport and your max at a specific large viewport.
Want font-size 16px at 320px viewport, 32px at 1280px viewport?
Rise: 32 − 16 = 16px over 1280 − 320 = 960px = 1.667% per px of viewport = 1.667vw (since 1vw = 1% of viewport).
Offset: at viewport 320px, 1.667vw = 5.33px. You want 16px, so add 16 − 5.33 = 10.67px ≈ 0.667rem.
Expression: clamp(1rem, 0.667rem + 1.667vw, 2rem).
The 0.667rem + 1.667vw reaches exactly 1rem (16px) at 320px and 2rem (32px) at 1280px. Beyond that, the clamp kicks in.
Beyond typography
Spacing:
padding: clamp(1rem, 5vw, 3rem);
Hero sections get more breathing room on desktop without crushing mobile.
Grid gap:
gap: clamp(0.5rem, 2vw, 2rem);
Border radius:
border-radius: clamp(4px, 1vw, 16px);
Section max-width:
max-width: clamp(400px, 80vw, 1200px);
Anywhere a length value varies with context, clamp() can replace multiple declarations.
Using other units in clamp
clamp() works with any length units — px, rem, em, %, vw, vh, ch, ex, and container query units (cqi, cqw). Mix freely:
width: clamp(300px, 50%, 800px); font-size: clamp(1rem, 5cqi, 2rem); /* container queries */
Container query units are especially powerful paired with @container — let components fluidly scale within their own container, not the viewport.
clamp() vs media queries
Use clamp() when a value scales smoothly with viewport size — typography, spacing, gap, proportional sizing.
Use media queries when layout changes structure — column count, hidden/shown elements, flex direction, different components entirely.
They coexist. A component with font-size: clamp(...) inside a media-query-driven grid is completely normal.
Accessibility — minimum matters
Users often set a minimum browser font size for readability.clamp() with a small vw-only preferred value can collapse below the user’s minimum on tiny viewports.
Rule: use a rem-based component in the preferred value, not pure vw. That way user zoom and base font-size changes continue to scale:
/* Good */ font-size: clamp(1rem, 0.8rem + 1.5vw, 1.5rem); /* Bad — ignores user's base font */ font-size: clamp(16px, 4vw, 24px);
Generator tools exist for a reason
The preferred-value math is error-prone. Plug your target sizes and breakpoints into a generator and get the expression. Plenty of free ones, including our own CSS clamp generator.
Bookmark one — you’ll use it every time you build a fluid design system.
Browser support
clamp() is universal since 2020. Chrome 79+, Firefox 75+, Safari 13.1+. Not a concern in 2026.
Container query units (cqi/cqw): Chrome 105+, Firefox 110+, Safari 16+. Safe for most projects now.
Debugging clamp
Inspect the element in devtools. Chrome and Firefox show the computed value at current viewport and highlight the clamp expression. Adjust the window and watch it update.
Pitfall: if your preferred value is stuck at min or max across all viewports, either your breakpoints don’t hit or your math is off. A generator catches this.
Common mistakes
Using only vw in preferred. Ignores user zoom and base font changes. Use rem + vw.
Setting min too low. Text collapses to unreadable on mobile. Min should be your intended mobile value, not smaller.
Setting max too high. Text or spacing balloons on wide monitors. Max should be your intended desktop value.
Using clamp for things that should be fixed.Button padding usually doesn’t need fluid scaling.
Combining with px-based media queries inconsistently.Pick rem-based breakpoints and stick with them; clamp() plays nicer with rems.
Doing the math by hand repeatedly. Use a generator. You’ll save hours.
Run the numbers
Generate a clamp() expression from min/max sizes and viewport range with the CSS clamp generator. Pair with the CSS minifier to ship the final stylesheet, and the CSS gradient generator to pair fluid type with visual polish.
Use these while you read
Tools that pair with this guide
- CSS Clamp GeneratorGenerate responsive CSS clamp() values for perfectly fluid typography by entering your minimum and maximum screen widths. Free, instant result.Developer Utilities
- CSS MinifierMinify your CSS online instantly by pasting it in to strip comments and whitespace for a smaller file. A free tool that keeps your data safe, no signup.Developer Utilities
- CSS Gradient GeneratorBuild and preview linear and radial CSS gradients with multiple color stops and custom angles. Copy the clean CSS code instantly with this free online generator.Developer Utilities
- JSON FormatterPaste JSON to beautify, validate, and minify with clear error messages, all in your browser without sign-up—free instant tool for developers.Developer Utilities
Advertisement
Continue reading
- Developers & TechnicalGitHub Actions Without Being a DevOps ExpertMaster GitHub Actions for the 90% use case with this practical playbook. Build, test, and deploy instantly using free common templates and no-sign-up guides.
- Developers & TechnicalBest Practices for Building Developer ToolsLearn CI/CD, IDE, and documentation standards for paid dev tools instantly. Implement best practices for what companies actually buy online.
- Developers & TechnicalHow to Contribute to Open Source Developer ToolsFind beginner-friendly OSS projects and ship your first pull request with confidence. Free, instant playbook to avoid mistakes and scale contributions.
- Developers & TechnicalHow to Design CLI Tools Developers LoveFree guide to build CLI tools developers actually love: composability, sensible defaults, human errors, trust by default, predictability, fast feedback.
- Developers & TechnicalPassword Security Guide with Real Entropy ExamplesCalculate real password entropy with 2026 attacker speeds. Free guide to diceware passphrases, password managers, and 2FA based on actual attack vectors.
- Developers & TechnicalJSON Format Rules Every Developer Should KnowFree guide to strict JSON spec rules, JSON5 vs JSONC, top 10 parser errors, Schema validation, streaming huge files, and security: prototype pollution.