Developer guide

Fluid Typography with CSS clamp(): A Practical Guide

How CSS clamp() makes font sizes grow with the screen: the formula, why rem matters for accessibility, a ready fluid type scale and use in Shopify themes.

Responsive typography used to mean a stack of media queries: 32px on mobile, 40px on tablets, 56px on desktop. Fluid typography replaces those jumps with one line of CSS that scales smoothly with the screen — using clamp().

How clamp() works

font-size: clamp(MIN, PREFERRED, MAX);

The browser uses the preferred value, but never goes below MIN or above MAX. When the preferred value contains a viewport unit (vw), it grows with the screen width — and the min and max stop it from getting too small on phones or too large on big monitors.

h1 { font-size: clamp(2rem, 1.47rem + 2.25vw, 3.5rem); }

This heading is 32px on a 375px phone, grows steadily, and reaches 56px at 1440px wide.

The formula

To go from min size at min viewport to max size at max viewport:

  • slope = (maxSize − minSize) ÷ (maxViewport − minViewport)
  • intercept = minSize − slope × minViewport
  • preferred = intercept (in rem) + slope × 100 (in vw)

You don’t need to do this by hand — the CSS Clamp Generator calculates it, shows a live preview and writes a whole type scale.

Why rem matters for accessibility

A size made only of vw ignores the visitor’s browser font-size setting and doesn’t grow when they zoom text, which fails WCAG success criterion 1.4.4 (Resize text). Using rem for the minimum, maximum and the intercept keeps text zoomable. Convert pixel sizes to rem with the PX to REM converter.

A fluid type scale

Instead of one-off values, define a scale once as CSS custom properties (here: 16→18px body text, ratio 1.2 on phones and 1.333 on desktop, 375→1440px viewports):

:root {
  --step-0: clamp(1rem, 0.96rem + 0.19vw, 1.125rem);
  --step-1: clamp(1.2rem, 1.09rem + 0.45vw, 1.5rem);
  --step-2: clamp(1.44rem, 1.24rem + 0.84vw, 2rem);
  --step-3: clamp(1.728rem, 1.4rem + 1.41vw, 2.667rem);
}
body { font-size: var(--step-0); }
h3 { font-size: var(--step-1); }
h2 { font-size: var(--step-2); }
h1 { font-size: var(--step-3); }

The generator’s “Fluid type scale” panel creates this for any base size and ratio.

Fluid spacing too

clamp() works for any length: section padding, gaps and margins.

.section { padding-block: clamp(2.5rem, 1.5rem + 4vw, 6rem); }

Generous spacing on desktop, compact on phones — no media queries.

Using clamp() in Shopify themes

Add the variables to your theme’s base stylesheet or a <style> block in theme.liquid, then use them in your sections. To let merchants choose sizes in the theme editor, expose a range setting with the Section Schema Generator and use it as the max value. Choose typefaces that suit fluid sizes with the Font Pair Generator.

Frequently asked questions

What does clamp() do in CSS?

clamp(MIN, PREFERRED, MAX) returns the preferred value, but never less than MIN or more than MAX. With a vw-based preferred value, the size scales with the screen between the two limits.

Is fluid typography accessible?

Yes, if the min and max are in rem and the preferred value mixes rem and vw. Pure vw sizes don’t respond to browser zoom, which fails WCAG 1.4.4.

Does clamp() work in all browsers?

Yes. clamp() is supported in every modern browser.