Most responsive headings I come across look like this:
<h1 class="text-3xl md:text-4xl lg:text-5xl">
It works. But resize the window slowly and you'll see the heading snap at 768px and again at 1024px. In between, it's always a little too big or a little too small for the space it's in.
clamp() fixes that with one class and no breakpoints:
<h1 class="text-[clamp(1.875rem,1.4789rem+1.6901vw,3rem)]">
That heading is 30px on a 375px phone, 48px on a 1440px laptop, and grows in a straight line in between.
What clamp() is doing
font-size: clamp(MIN, PREFERRED, MAX);
The browser uses the preferred value, but never goes below the minimum or above the maximum. For fluid type, the preferred value is a fixed part in rem plus a part that grows with the screen in vw:
font-size: clamp(1.875rem, 1.4789rem + 1.6901vw, 3rem);
On a narrow phone, the middle value works out smaller than 1.875rem, so the minimum wins. On a wide screen it'd be bigger than 3rem, so the maximum wins. In between, you get the smooth part.
Where those odd numbers come from
Say you want 30px at a 375px screen and 48px at 1440px.
-
Slope. How much the font grows per pixel of screen:
(48 − 30) / (1440 − 375) = 0.0169 -
The vw part. 1vw is 1% of the screen, so multiply by 100:
1.6901vw -
The rem part. Where the line would cross a zero-width screen, divided by 16:
(30 − 0.0169 × 375) / 16 = 1.4789rem -
Min and max.
30 / 16 = 1.875rem,48 / 16 = 3rem
Put together: clamp(1.875rem, 1.4789rem + 1.6901vw, 3rem).
Two things that caught me out:
-
Tailwind arbitrary values can't have spaces.
text-[clamp(1.875rem, 1.4789rem + 1.6901vw, 3rem)]gets split into several broken classes. Squash it:text-[clamp(1.875rem,1.4789rem+1.6901vw,3rem)]. -
Shrinking sizes flip everything. If something should get smaller on bigger screens (it happens with spacing), the slope goes negative. An early version of my calculator happily printed
+ -1.69vw, which is ugly but still valid. The real trap is the order. The smaller size must be the first argument toclamp(). If the minimum is bigger than the maximum, the minimum always wins, and your "fluid" size never changes at all.
The zoom catch
You might wonder why not just write font-size: 4vw. It's fluid, after all.
The problem is that vw ignores the reader. If someone bumps their browser's default font size, or zooms in because they can't read your text, a pure vw size barely moves. That fails WCAG 1.4.4, which asks that text can be resized to 200%.
The rem part of the formula, and rem for the min and max, keep the text tied to the user's settings, so zoom keeps working.
There's one more limit that's easy to miss. If your largest size is more than about 2.5 times your smallest, zooming in may still not reach 200%, because the vw part doesn't grow with zoom. Keeping the ratio under 2.5 is a good rule. Every size in the table below is under 2.
7 sizes you can copy
These use Tailwind's default scale for the min and max, a 16px root, and the screen widths most designs start from.
| Use | Sizes | Screens | Class |
|---|---|---|---|
| Hero title | text-4xl → text-7xl (36 → 72px) | 375–1440px | text-[clamp(2.25rem,1.4577rem+3.3803vw,4.5rem)] |
| Page title | text-3xl → text-5xl (30 → 48px) | 375–1440px | text-[clamp(1.875rem,1.4789rem+1.6901vw,3rem)] |
| Section title | text-2xl → text-4xl (24 → 36px) | 375–1280px | text-[clamp(1.5rem,1.1892rem+1.326vw,2.25rem)] |
| Card title | text-lg → text-2xl (18 → 24px) | 375–1280px | text-[clamp(1.125rem,0.9696rem+0.663vw,1.5rem)] |
| Body text | text-base → text-lg (16 → 18px) | 375–1440px | text-[clamp(1rem,0.956rem+0.1878vw,1.125rem)] |
| Caption | text-xs → text-sm (12 → 14px) | 375–1440px | text-[clamp(0.75rem,0.706rem+0.1878vw,0.875rem)] |
| Button | text-sm → text-base (14 → 16px) | 375–1280px | text-[clamp(0.875rem,0.8232rem+0.221vw,1rem)] |
Making it nicer in Tailwind v4
Those classes are long, and you don't want to paste them around. In Tailwind v4 you can name them once in @theme:
/* app.css */
@import "tailwindcss";
@theme {
--text-fluid-title: clamp(1.875rem, 1.4789rem + 1.6901vw, 3rem);
}
<h1 class="text-fluid-title">
On Tailwind v3, the same value goes under theme.extend.fontSize in tailwind.config.js.
The same trick works for spacing, too. p-[clamp(...)] or a --spacing-* token gives you padding that breathes with the screen.
If you don't want to do the maths
Working these out by hand gets old fast, so I built a Tailwind clamp() calculator. Pick two Tailwind sizes or type px/rem, drag the screen width to watch it on a graph, and copy the class, a v4 @theme token or plain CSS. It also warns you when the range is too wide for that 200% zoom rule.
What range do you use for fluid headings? I'm curious if anyone goes beyond 375–1440.
I wrote this with help from an AI assistant for drafting and editing. The formula, the table values and the calculator come from my own project, AppyGrid.
Top comments (0)