WebTech1223411 logo WebTech1223411Web tech, read closely
Front-End

CSS Clamp for Fluid Typography Without Media Queries

Your headings look right at 1440 pixels, cramped at 768 and absurdly large on a 390 pixel phone, so you add another media query, then another.

A developer's desk with a wide monitor and a phone side by side showing the same article page with headings scaled to each screen

Your headings look right at 1440 pixels, cramped at 768 and absurdly large on a 390 pixel phone, so you add another media query, then another. Fluid typography with CSS clamp() replaces that stack of breakpoints with one line per text style that scales smoothly between a minimum and a maximum size.

I have moved three production design systems to this approach. It cut our type related CSS by more than half and removed a whole class of "it looks odd at 900 pixels" bugs. Here is how to set it up properly, including the accessibility trap that most tutorials skip.

How CSS clamp() works for fluid typography

The clamp() function takes three values: a minimum, a preferred value and a maximum. The browser uses the preferred value unless it falls outside the range, in which case it uses the nearest limit.

A typical declaration looks like this: font-size: clamp(1.5rem, 1.1rem + 2vw, 2.5rem). On a narrow phone the heading stays at 1.5rem. On a wide desktop it stops at 2.5rem. In between, it grows with the viewport width because of the vw part of the middle value.

Notice the middle value mixes rem and vw. That detail matters, and we will come back to it in the accessibility section.

Calculating the preferred value step by step

Guessing the middle value leads to text that hits its maximum too early or never reaches it. The calculation is simple linear interpolation. Say you want a heading to be 24 pixels at a 360 pixel viewport and 40 pixels at a 1280 pixel viewport.

  1. Find the slope. (40 minus 24) divided by (1280 minus 360) equals 16 divided by 920, which is about 0.0174.
  2. Convert the slope to vw. Multiply by 100 to get 1.74vw.
  3. Find the intercept. 24 minus (0.0174 times 360) equals about 17.74 pixels.
  4. Convert the intercept to rem. Divide by 16 to get about 1.109rem.
  5. Write the rule. clamp(1.5rem, 1.109rem + 1.74vw, 2.5rem).

Check it: at 360 pixels, 17.74 plus 6.26 is 24. At 1280 pixels, 17.74 plus 22.27 is about 40. The type now scales exactly across the range you chose.

Do this once per step in your type scale and keep the results in a small table in your design docs.

A practical fluid type scale

Here is a scale I have used on content sites, based on a 360 to 1280 pixel range.

StyleSize at 360pxSize at 1280pxclamp() value
Small text14px15pxclamp(0.875rem, 0.85rem + 0.11vw, 0.9375rem)
Body16px18pxclamp(1rem, 0.951rem + 0.22vw, 1.125rem)
H319px24pxclamp(1.1875rem, 1.065rem + 0.54vw, 1.5rem)
H224px32pxclamp(1.5rem, 1.304rem + 0.87vw, 2rem)
H130px48pxclamp(1.875rem, 1.435rem + 1.96vw, 3rem)

The ratio between steps is tighter on small screens and wider on large ones. That is intentional. A big jump from body to H1 looks dramatic on a desktop but wastes half the screen on a phone.

If you set these values as custom properties on the root element, keep them in a dedicated layer so component styles cannot accidentally override them. Our walkthrough of how cascade layers tame a growing stylesheet shows a layer order that works well for tokens like these.

The accessibility trap with pure vw values

Many examples online use something like clamp(1rem, 4vw, 2rem). It looks tidy, but the middle value is pure viewport units. When a user zooms the page on desktop, each CSS pixel gets bigger, so the viewport becomes narrower in CSS pixels and a vw based size shrinks by the same factor. On screen, the text stays the same physical size and refuses to grow.

WCAG asks that text can be resized to 200 percent without loss of content. Text sized only in vw can fail that test. The fix is the pattern used above: always mix a rem value into the preferred size, such as 1rem + 1vw. The rem part responds to zoom and to the user's default font size setting, and the vw part handles the fluid scaling.

A quick test: set your browser zoom to 200 percent on a 1280 pixel window and confirm headings and body text roughly double. If they barely move, your preferred value relies too heavily on vw.

Where I would still use a media query

The common advice is that fluid typography means you never need breakpoints for type again. I do not fully agree.

Fluid scaling handles size well. It does not handle line length, line height or letter spacing on its own. A 48 pixel H1 usually wants a line height around 1.1, while the same heading at 30 pixels reads better at 1.2 or 1.25. Long display headings may also need a tighter max width on wide screens to avoid lines of 15 words. A single media query at around 900 pixels that adjusts line height and max width is still the cleanest tool for that job.

For components that live in sidebars or cards of different widths, viewport based sizing can also be wrong, because the component's space is not the viewport. In that case, container queries and container units let a card's heading scale with the card itself. You can use cqi units inside clamp() exactly as you would vw.

Testing fluid typography before you ship

  • Drag the window from 320 to 1920 pixels and watch for jumps. Smooth scaling means the math is right.
  • Check the minimum on the smallest real device you support, not only in DevTools.
  • Zoom to 200 percent and confirm text grows.
  • Change the browser default font size to 20 pixels and confirm the whole scale shifts up.
  • Look at long German or Finnish words in headings, since they overflow first on narrow screens.

Clamp() is supported in all current major browsers, so there is no need for a fallback in most projects. It is one of several features covered in what modern CSS can do now that used to need JavaScript.

Frequently asked questions

What is fluid typography in CSS?

Fluid typography means font sizes scale smoothly with the screen or container size instead of jumping at fixed breakpoints. In CSS it is usually written with clamp(), which sets a minimum, a preferred scaling value and a maximum. The result is readable text at every width with less code.

Is clamp() for font size bad for accessibility?

Not if you build the preferred value correctly. Pure vw values can stop text from growing when users zoom. Mixing rem with vw, as in 1rem + 1vw, keeps text responsive to zoom and to the user's font size setting.

How do you calculate the middle value of clamp()?

Pick a minimum and maximum font size and the viewport widths where each should apply. Divide the size difference by the width difference to get the slope, multiply by 100 for the vw value, then find the intercept and convert it to rem. Add the two parts together as the preferred value.

LV
Lorenzo Vidal

Lorenzo started as a visual designer and moved into CSS after watching too many of his layouts fall apart on phones. He writes about front-end craft, interfaces and accessibility, and still tests everything with the keyboard first.

More posts by Lorenzo

More in Front-End

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp foxstudiosscratch gamevui123 gleasongrade greetingsday hacklabasu haskell iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten qvantor rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot skrtips spotlesshaven strcoding tetapatrium topselller uneplanteartificielle vipertechcr wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto