Frontend development · Intermediate · 20 min
Build fluid type with CSS clamp()
By Padding · Updated 18 Nov 2026
Modern browsers · CSS Values and Units Level 4 clamp() · checked in Chromium 159
You will turn the Larkfield Clinic type scale into CSS tokens whose heading sizes grow smoothly from a 360 px phone to a 1280 px desktop, work out each clamp() value by hand, and prove with a script that the sizes are right at each width, at 200% zoom and with a larger browser text size.
On this page
Larkfield Clinic's booking app has a type scale built for phones: display 31 px for the appointment time, heading-l 25 px for screen titles, heading-m 20 px for header and day headings, then body 16, body-small 14 and label 14. On a 360 px phone those sizes are right. On a 1280 px laptop the same 25 px title looks timid in a wide column, and a media query that jumps it to 31 px at one breakpoint makes the page lurch as the window crosses that line.
Fluid type removes the jump. A heading's size becomes a straight line between two points, "25 px at 360 wide" and "31 px at 1280 wide", and CSS clamp() stops it going past either end. Plenty of online generators will write the value for you. Here you do the arithmetic yourself, once, so you can read and fix any clamp() you meet, and you check two things a generated value will not tell you on its own: that text still follows the reader's own text-size setting, and how much the headings grow at 200% zoom.
You end with a tokens.css file of type tokens, a specimen page with real Larkfield copy, a small calculator script and a report script that prints every role's size at each width.
Step 1 · Build a specimen page with fixed sizes
Start from the scale as it stands: fixed sizes, all in rem so they follow the browser's text-size setting. Make a demo folder and the page:
mkdir -p ~/fluid-type-demo
cd ~/fluid-type-demo
cat > index.html <<'HTML'
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Larkfield type specimen</title>
<link rel="stylesheet" href="tokens.css">
<link rel="stylesheet" href="page.css">
</head>
<body>
<p class="readout">Window: <span id="width"></span> px wide</p>
<main class="specimen">
<p class="display" data-role="display">10:30</p>
<h1 class="heading-l" data-role="heading-l">Check your booking</h1>
<h2 class="heading-m" data-role="heading-m">Tue 9 Dec</h2>
<p class="body" data-role="body">We'll text you a reminder the day before.</p>
<p class="body-small" data-role="body-small">Nurse appointment · 15 min</p>
<p class="label" data-role="label">Book appointment</p>
<p class="vw-only" data-role="vw-only">10:30</p>
<p class="px-clamp" data-role="px-clamp">10:30</p>
</main>
<script>
function show() {
document.getElementById('width').textContent = innerWidth;
for (const el of document.querySelectorAll('[data-role]')) {
el.dataset.size = parseFloat(getComputedStyle(el).fontSize).toFixed(2);
}
}
addEventListener('resize', show);
show();
</script>
</body>
</html>
HTMLThe small script writes each line's computed font size into a data-size attribute, and the stylesheet prints it after the text, so you can read sizes without opening developer tools. The last two lines, vw-only and px-clamp, are for a comparison in a later step; until then they show at body size.
Now the tokens. Each role gets a size and a line height, named --type-<role>-<property> to match the Figma variables from the type scale tutorial. Every line height is a multiple of 4 px:
cat > tokens.css <<'CSS'
:root {
/* Larkfield type tokens: phone sizes, fixed */
--type-display-size: 1.9375rem; /* 31px */
--type-display-line-height: 2.5rem; /* 40px */
--type-heading-l-size: 1.5625rem; /* 25px */
--type-heading-l-line-height: 2rem; /* 32px */
--type-heading-m-size: 1.25rem; /* 20px */
--type-heading-m-line-height: 1.5rem; /* 24px */
--type-body-size: 1rem; /* 16px */
--type-body-line-height: 1.5rem; /* 24px */
--type-body-small-size: 0.875rem; /* 14px */
--type-body-small-line-height: 1.25rem; /* 20px */
--type-label-size: 0.875rem; /* 14px */
--type-label-line-height: 1.25rem; /* 20px */
}
CSS
cat > page.css <<'CSS'
body {
margin: 0;
padding: 1rem;
font-family: Inter, system-ui, sans-serif;
color: #1b1f24;
}
.specimen > * { margin: 0 0 1.75rem; }
.display { font-size: var(--type-display-size); line-height: var(--type-display-line-height); font-weight: 600; }
.heading-l { font-size: var(--type-heading-l-size); line-height: var(--type-heading-l-line-height); font-weight: 600; }
.heading-m { font-size: var(--type-heading-m-size); line-height: var(--type-heading-m-line-height); font-weight: 600; }
.body { font-size: var(--type-body-size); line-height: var(--type-body-line-height); }
.body-small { font-size: var(--type-body-small-size); line-height: var(--type-body-small-line-height); }
.label { font-size: var(--type-label-size); line-height: var(--type-label-line-height); font-weight: 600; }
.readout, [data-role]::after { font: 400 0.875rem/1.25rem system-ui, sans-serif; color: #5a6270; }
[data-role]::after { content: " " attr(data-role) " " attr(data-size) "px"; }
CSSThe rem values are the pixel sizes divided by 16, the browser's default text size. Page styles only ever use the tokens, so the next steps change tokens.css and nothing else.
Check it worked
Open index.html in your browser (drag the file into a window, or run open index.html on macOS or xdg-open index.html on Linux). The top line gives the window width. Each specimen line ends with its role and size in grey: display 31.00px, heading-l 25.00px, heading-m 20.00px, body 16.00px, body-small 14.00px and label 14.00px. Drag the window narrower and wider: the width changes, the sizes do not.
Step 2 · Choose the two widths and the size at each end
A fluid size needs two anchor points: a narrow width with its size and a wide width with its size. Larkfield uses 360 px, a common width for budget Android phones and the narrowest the app is designed for, and 1280 px, a common laptop width. Below 360 and above 1280 the sizes stop changing.
At the narrow end, keep the phone scale exactly as it is. At the wide end, move each heading role up one step of the same 1.25 ratio scale (13, 16, 20, 25, 31, 39), so the desktop sizes are values the scale already contains:
display: 31 px at 360, 39 px at 1280heading-l: 25 px at 360, 31 px at 1280heading-m: 20 px at 360, 25 px at 1280
Line heights follow the scale's rule, size × 1.2 rounded up to a multiple of 4: 39 × 1.2 = 46.8 gives 48, 31 × 1.2 = 37.2 gives 40, and 25 × 1.2 = 30 gives 32. So the line heights run 40 to 48, 32 to 40 and 24 to 32.
body, body-small and label stay fixed. Body text is read at the same distance on a phone and a laptop, so it does not need to grow, and a wider column is better handled by limiting line length than by enlarging the letters. Keeping the small roles fixed also means the 14 px floor can never be undercut.
Keep the gap between the two ends modest. Here the largest change is 39 ÷ 31, about 1.26 times. You will see in the zoom step why a big gap is a problem.
Check it worked
Write the six pairs (three sizes, three line heights) somewhere you can see them. Every desktop size is in the list 13, 16, 20, 25, 31, 39, every line height at both ends divides by 4, and no wide-end value is more than about 1.3 times its narrow-end value.
Step 3 · Work out the preferred value by hand
clamp(MIN, PREFERRED, MAX) returns the preferred value unless it falls below the minimum or above the maximum. MIN and MAX are your two end sizes. PREFERRED is the straight line through the two anchor points, written as a fixed part plus a part that depends on the window width.
Take display, from 31 px at 360 to 39 px at 1280.
- Slope: how much the size changes per pixel of width. (39 − 31) ÷ (1280 − 360) = 8 ÷ 920 = 0.0086957.
- Turn the slope into
vw. Onevwis 1% of the window width, so a slope of 0.0086957 px per px is 0.0086957 × 100 = 0.8696vw. - Intercept: where the line would be at a window 0 px wide. 31 − 0.0086957 × 360 = 31 − 3.1304 = 27.8696 px.
- Turn the intercept into
rem: 27.8696 ÷ 16 = 1.7418rem. - Put it together, with the end sizes in rem too:
clamp(1.9375rem, 1.7418rem + 0.8696vw, 2.4375rem).
Plug the widths back in to check. At 360 px, 0.8696vw is 3.13 px, and 27.87 + 3.13 = 31. At 1280 px, 0.8696vw is 11.13 px, and 27.87 + 11.13 = 39.
Doing that six times by hand invites slips, so put the same five steps in a tiny script and use it to check your working:
cat > fluid.mjs <<'JS'
// Usage: node fluid.mjs <px at 360 wide> <px at 1280 wide>
const [min, max] = process.argv.slice(2).map(Number);
const [w1, w2] = [360, 1280];
const slope = (max - min) / (w2 - w1); // px of size per px of width
const intercept = min - slope * w1; // size at a 0px wide window
const rem = (px) => +(px / 16).toFixed(4) + 'rem';
console.log(`clamp(${rem(min)}, ${rem(intercept)} + ${(slope * 100).toFixed(4)}vw, ${rem(max)})`);
JS
node fluid.mjs 31 39
node fluid.mjs 40 48
node fluid.mjs 25 31
node fluid.mjs 32 40
node fluid.mjs 20 25
node fluid.mjs 24 32Four decimal places is plenty: the rounding error is well under a hundredth of a pixel.
Check it worked
Before reading the output, work out heading-m (20 to 25) on paper: the slope is 5 ÷ 920 = 0.0054348, so 0.5435vw, and the intercept is 20 − 1.9565 = 18.0435 px, or 1.1277rem. The script prints six lines, and the fifth reads clamp(1.25rem, 1.1277rem + 0.5435vw, 1.5625rem), matching your working. The first reads clamp(1.9375rem, 1.7418rem + 0.8696vw, 2.4375rem).
Step 4 · Write the fluid tokens
Replace tokens.css with the fluid version. The three heading roles take the six clamp() values from the script; the three fixed roles are unchanged:
cat > tokens.css <<'CSS'
:root {
/* Larkfield type tokens. Fluid roles grow from 360px to 1280px wide. */
--type-display-size: clamp(1.9375rem, 1.7418rem + 0.8696vw, 2.4375rem); /* 31px to 39px */
--type-display-line-height: clamp(2.5rem, 2.3043rem + 0.8696vw, 3rem); /* 40px to 48px */
--type-heading-l-size: clamp(1.5625rem, 1.4158rem + 0.6522vw, 1.9375rem); /* 25px to 31px */
--type-heading-l-line-height: clamp(2rem, 1.8043rem + 0.8696vw, 2.5rem); /* 32px to 40px */
--type-heading-m-size: clamp(1.25rem, 1.1277rem + 0.5435vw, 1.5625rem); /* 20px to 25px */
--type-heading-m-line-height: clamp(1.5rem, 1.3043rem + 0.8696vw, 2rem); /* 24px to 32px */
/* Fixed roles: body text stays put */
--type-body-size: 1rem; /* 16px */
--type-body-line-height: 1.5rem; /* 24px */
--type-body-small-size: 0.875rem; /* 14px */
--type-body-small-line-height: 1.25rem; /* 20px */
--type-label-size: 0.875rem; /* 14px */
--type-label-line-height: 1.25rem; /* 20px */
}
CSSLine heights are fluid too, so the 4 px rhythm holds exactly at both ends. In between, a heading's line box is something like 43.6 px; nothing lines up against a heading's baseline mid-range, so that is fine.
Check it worked
Reload the page and drag the window edge slowly. The three heading sizes change smoothly as the width changes, with no jump. With the window narrower than 360 px (use your browser's responsive device mode), display reads 31.00px; wider than 1280 px, it reads 39.00px. The body, body-small and label lines stay at 16, 14 and 14 throughout.
Step 5 · Write a script that reports sizes at each width
Dragging a window is a rough check. A short Playwright script opens the page in a real Chromium at exact widths and prints each role's size, so you can rerun it whenever the tokens change. Install Playwright in the demo folder, pinned to a known version, and the browser it drives:
cd ~/fluid-type-demo
npm init -y > /dev/null
npm install --save-dev playwright@1.65.1
npx playwright install chromiumOn Linux, if Chromium will not start because of missing system libraries, run sudo npx playwright install-deps chromium once.
The script loads the page at each case and prints the on-screen size of every role: the CSS pixel size times the zoom level, which is what the reader's eye sees. Two of the cases emulate browser zoom. Zooming a 1280 px window to 200% makes every CSS pixel two screen pixels, so the page lays out as if the window were 640 CSS pixels wide; the script reproduces that with a 640 px viewport and a pixel ratio of 2. The last case emulates a reader who has set the browser's default text size to 32 px:
cat > report.mjs <<'JS'
// Prints each role's on-screen size in pixels (CSS pixels times zoom).
import { chromium } from 'playwright';
import { pathToFileURL } from 'node:url';
import { resolve } from 'node:path';
const url = pathToFileURL(resolve('index.html')).href;
const roles = ['display', 'heading-l', 'heading-m', 'body', 'body-small', 'label', 'vw-only', 'px-clamp'];
const cases = [
{ name: 'phone 360', width: 360, zoom: 1 },
{ name: 'tablet 768', width: 768, zoom: 1 },
{ name: 'desktop 1280', width: 1280, zoom: 1 },
{ name: '1280 at 200% zoom', width: 640, zoom: 2 },
{ name: '1280 at 250% zoom', width: 512, zoom: 2.5 },
{ name: '1280, text size 32px', width: 1280, zoom: 1, textSize: 32 },
];
const browser = await chromium.launch();
console.log('case'.padEnd(22) + roles.map((r) => r.padStart(11)).join(''));
for (const c of cases) {
const page = await browser.newPage({ viewport: { width: c.width, height: 800 }, deviceScaleFactor: c.zoom });
await page.goto(url);
if (c.textSize) await page.addStyleTag({ content: `html { font-size: ${c.textSize}px; }` });
const sizes = await page.evaluate((list) => list.map((r) =>
parseFloat(getComputedStyle(document.querySelector(`[data-role="${r}"]`)).fontSize)), roles);
console.log(c.name.padEnd(22) + sizes.map((s) => (s * c.zoom).toFixed(2).padStart(11)).join(''));
await page.close();
}
await browser.close();
JS
node report.mjsSetting font-size on html is how the script stands in for the browser setting: rem is measured from the root element's size, and the browser's default text size is what the root element gets when nothing overrides it.
Check it worked
The first three rows of the output read like this (the last two columns are still at body size, because their styles come in the next step):
case display heading-l heading-m body body-small label vw-only px-clamp
phone 360 31.00 25.00 20.00 16.00 14.00 14.00 16.00 16.00
tablet 768 34.55 27.66 22.22 16.00 14.00 14.00 16.00 16.00
desktop 1280 39.00 31.00 25.00 16.00 14.00 14.00 16.00 16.00The 360 and 1280 rows match your end sizes exactly, and 768 sits in between. You will read the last three rows in the next two steps.
Step 6 · Keep a rem part so text follows the reader's setting
The fixed part of the preferred value is in rem for a reason. To see it, add the two common shortcuts as comparison rows: a size in vw alone, chosen to hit 39 px at 1280, and the same clamp() maths written in px:
cat >> page.css <<'CSS'
/* Two ways not to do it, for comparison */
.vw-only { font-size: 3.0469vw; line-height: 1.25; font-weight: 600; }
.px-clamp { font-size: clamp(31px, 27.8688px + 0.8696vw, 39px); line-height: 1.25; font-weight: 600; }
CSS
node report.mjsNow read the vw-only and px-clamp columns against display, which has the same targets:
case display heading-l heading-m body body-small label vw-only px-clamp
phone 360 31.00 25.00 20.00 16.00 14.00 14.00 10.97 31.00
tablet 768 34.55 27.66 22.22 16.00 14.00 14.00 23.40 34.55
desktop 1280 39.00 31.00 25.00 16.00 14.00 14.00 39.00 39.00
1280 at 200% zoom 66.87 53.65 43.04 32.00 28.00 28.00 39.00 66.87
1280 at 250% zoom 80.80 64.98 52.06 40.00 35.00 35.00 39.00 80.80
1280, text size 32px 66.87 53.65 43.04 32.00 28.00 28.00 39.00 39.00vw-onlyis 11 px on a phone, and it never grows when the reader zooms or raises the text size. Zooming shrinks the viewport in CSS pixels, so a size made only ofvwshrinks by the same amount and cancels the zoom out.px-clampbehaves likedisplayunder zoom, because zoom scales pixels too. But it ignores the text-size setting: a reader who asked for text twice as large still gets 39 px.display, built fromremplusvwwithremlimits, responds to both.
Readers who need larger text often raise it in settings rather than zooming each page. How that reaches your CSS depends on the device. Desktop browsers have a default font size setting, and rem follows it directly, which is the case the last row tests. On phones, a raised system text size mostly arrives as page zoom or text scaling instead, which makes the CSS viewport narrower than the screen: a 360 px budget Android phone, which is how many Larkfield patients book, can end up laying the page out below 360 CSS pixels. That is under the narrow anchor, so every fluid role sits at its rem minimum and grows in full with the reader's setting.
Check it worked
Your output matches the table above. Then try it yourself in a desktop browser that has a default font size setting: in Chrome or Edge it is under Settings > Appearance > Font size, and in Firefox under Settings > General > Fonts. Safari only has a minimum font size, so on a Mac use Chrome or Firefox for this check. Raise the size and reload the page. The display, heading and body sizes all grow; vw-only and px-clamp stay where they were. Put the setting back afterwards.
Step 7 · Check the headings at 200% zoom
WCAG 2.2 success criterion 1.4.4, Resize Text, asks that text can be resized up to 200% without losing content or function. Body text here is fixed rem, so at 200% zoom it is exactly twice the size: 16 becomes 32 in the report. The fluid headings are different, and the reason is arithmetic.
With zoom level z on a window W screen pixels wide, the page is W ÷ z CSS pixels wide, so the on-screen size of a fluid role is:
z × (intercept + slope × W ÷ z) = z × intercept + slope × W
Only the rem intercept is multiplied by the zoom. The vw part stays the same screen size however far you zoom. For display on a 1280 px window that is 2 × 27.87 + 11.13 = 66.87 px at 200%, which is 1.71 times its 39 px. Push zoom to 250% and it is 80.80 px, 2.07 times. At 400% the page is only 320 CSS pixels wide, below the 360 anchor, so every heading falls back to its rem minimum and grows in full.
So with this scale, body text doubles at 200% and every heading doubles by 250%, both well within the zoom range desktop browsers offer, which goes past 250%. The bigger the vw part is compared with the rem part, the further the headings fall behind. That is why the previous steps kept each role's wide end close to its narrow end: a heading that went from 20 px to 60 px would be mostly vw and would barely react to zoom.
Check it worked
In the report, the 1280 at 200% zoom row shows body at 32.00 and display at 66.87, and the 1280 at 250% zoom row shows each heading at more than twice its desktop 1280 value (80.80 against 39, 64.98 against 31, 52.06 against 25). Then check by eye: widen your browser window to about 1280 px, press Ctrl and + (Cmd and + on macOS) until the zoom reads 200%, and look at the readout line. It says about 640 px wide, and display reads about 33.43px in CSS pixels, which your screen draws twice as large. Reset zoom with Ctrl and 0 (Cmd and 0).
Step 8 · Clean up
When you have copied tokens.css and fluid.mjs into your own project, clean up in this order. Playwright keeps its downloaded browsers in a shared cache outside the demo folder: ~/.cache/ms-playwright on Linux and ~/Library/Caches/ms-playwright on macOS. While the pinned Playwright is still installed in the demo folder, run its uninstall command first, then remove the folder and the package in it:
cd ~/fluid-type-demo
npx playwright uninstall
cd ~
rm -rf ~/fluid-type-demonpx playwright uninstall removes the browsers that no other Playwright install on your machine still uses. If you do not use Playwright anywhere else and want every downloaded browser gone, run npx playwright uninstall --all instead, before the rm -rf.
Check it worked
ls ~/fluid-type-demo reports that there is no such file or directory. If you used --all, the ms-playwright cache folder above is gone or empty too.
Next steps
- The sizes here come from Set a type scale for an app, which explains the 1.25 ratio, the 14 px floor and the 4 px line heights.
- Put these type tokens alongside Larkfield's colour tokens in two layers with Turn design tokens into CSS custom properties.
- Bigger text means longer screens and wrapped headings. Design layouts that survive large text covers what the components have to do about it.
- To generate the tokens from one file instead of writing them by hand, see Generate CSS from a token file with Style Dictionary.
Part of these paths
Related tutorials
- Set a type scale for an appYou will have a type scale for the Larkfield Clinic app worked out from a base size and a ratio, stored as Figma variables and six text styles named by role, shown on a specimen frame with real copy and checked at 200% text size.
- Turn design tokens into CSS custom propertiesYou will have a two-layer tokens.css file of primitive and semantic custom properties, component styles that use only the semantic layer, a Node script that enforces that rule, and a demo page that proves one primitive change reaches every component.
- Design layouts that survive large textYou will have the Larkfield Clinic booking screens tested at the largest phone text size and at 200% browser zoom, every truncated label, overlap and fixed-height box fixed so it wraps, stacks or scrolls, and each screen shown at default and large text side by side.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials