How-to GuidePublished 13 min read

How to Calculate CSS clamp(): Working Out the vw and rem Values, Fluid Font Sizes by Viewport Width with a Quick Reference Table, Finding How Many px a clamp() Gives, and Accessibility Pitfalls

16px on phones, 24px on desktops. Text that scales smoothly with the viewport width like this takes a single line with CSS clamp(): font-size: clamp(1rem, 0.8239rem + 0.7512vw, 1.5rem);. But if you do not know where the 0.8239rem and 0.7512vw in the middle come from, you end up hunting for new numbers every time the design changes. This article covers how to get the vw coefficient and the rem intercept from the minimum and maximum sizes and a viewport range, a quick reference table of clamp() values for common sizes, how to choose the viewport range, how to work out how many px an existing clamp() gives at each viewport width, the rule of thumb that keeps browser zoom (WCAG 1.4.4) working, fluid type scales that make all your headings scale at once, using clamp() for spacing and in Tailwind CSS, and when to use media queries instead. Every figure is calculated with the same formulas as the CSS clamp() Generator.

The short answer: the vw coefficient is “size difference ÷ viewport difference × 100”

To make a font size scale with the viewport width using clamp(), you only need to decide four numbers: the size on small screens, the size on large screens, and the viewport range over which it should change smoothly. Everything else follows by calculation. You draw a straight line relating viewport width to size, then write its slope in vw and its value at a viewport width of 0 in rem.

What you wantFormulaExample: 16 to 24px, viewport 375 to 1440px
Slope(maximum size - minimum size) ÷ (maximum viewport width - minimum viewport width)(24 - 16) ÷ (1440 - 375) ≈ 0.007512
vw coefficientslope × 1000.7512vw
Intercept (px)minimum size - slope × minimum viewport width16 - 0.007512 × 375 ≈ 13.18px
Intercept (rem)intercept (px) ÷ root font size (usually 16px)13.18 ÷ 16 ≈ 0.8239rem
clamp()clamp(minimum, intercept + coefficient vw, maximum)clamp(1rem, 0.8239rem + 0.7512vw, 1.5rem)

This clamp() stays at 16px at viewport widths of 375px and below and at 24px at 1440px and above, and grows in proportion to the viewport width in between. At 768px (an iPad in portrait, for example) it is 13.18 + 0.7512 × 7.68 ≈ 18.95px. Because 1vw is 1/100 of the viewport width, “0.7512vw” means “0.7512px larger for every 100px of viewport width”.

clamp() basics: what the three values mean and how it behaves

clamp(minimum, preferred, maximum) is a CSS function that keeps the preferred value between the minimum and the maximum. If the preferred value is smaller than the minimum, you get the minimum; if it is larger than the maximum, you get the maximum; in between, the preferred value itself is used. It behaves the same as max(minimum, min(preferred, maximum)) and works in all current major browsers (Chrome, Edge, Safari and Firefox).

  • Minimum: the value you do not want to go below on small screens, such as 1rem (16px) for text
  • Preferred: the part that changes with the viewport width. Written as “rem + vw”, it becomes a straight line that grows in proportion to the viewport width
  • Maximum: the value you do not want to go above on large screens. It stops headings from becoming huge on big monitors
  • If a combination makes the minimum larger than the maximum, the CSS rules say the minimum wins

It works not only for font-size but for any property that takes a length, such as padding, margin, gap and width. Only the vw part changes with the viewport width, so without vw in the preferred value nothing changes. On the other hand, a preferred value of vw alone causes the problem described in the zoom section below, so the basic rule is to keep a rem part.

Working out vw and rem: five steps, using a 32 to 48px heading

Suppose the design mockup says “h1 is 32px on phones (375px wide) and 48px on desktops (1440px wide)”. Here is how to calculate it by hand.

  1. 1

    Find the size difference and the viewport difference

    The size difference is 48 - 32 = 16px and the viewport difference is 1440 - 375 = 1065px.

  2. 2

    Find the slope and multiply by 100 to get vw

    The slope is 16 ÷ 1065 ≈ 0.015023. Multiplying by 100 gives 1.5023vw: about 1.5px larger for every 100px of viewport width.

  3. 3

    Find the intercept in px

    32 - 0.015023 × 375 ≈ 26.37px. This is the value if you extend the line back to a viewport width of 0; text is never actually shown at this size.

  4. 4

    Convert to rem

    Divide by the 16px root font size: the intercept is 26.37 ÷ 16 ≈ 1.6479rem, the minimum is 32 ÷ 16 = 2rem and the maximum is 48 ÷ 16 = 3rem.

  5. 5

    Put them into clamp() and check

    You get font-size: clamp(2rem, 1.6479rem + 1.5023vw, 3rem);. It is 32px at a 375px viewport, 48px at 1440px and about 37.90px at 768px.

Rounding to about four decimal places is plenty. Rounding 0.7512vw changes the result by only about 0.001px, even at a 1440px viewport. The CSS clamp() Generator also rounds its output to four decimal places.

On sites that set the root to 10px with html { font-size: 62.5%; }, the rem numbers change. The same 16 to 24px across 375 to 1440px becomes clamp(1.6rem, 1.3183rem + 0.7512vw, 2.4rem). The vw coefficient comes from the slope in px, so it does not change with the root font size. In the tool, set “Root font size (1rem)” to 10 to get the same result.

With px output instead of rem, you get clamp(16px, 13.1831px + 0.7512vw, 24px). The size at each viewport width is the same, but it makes a difference for people who raise the browser's font size setting from 16px to 20px. The rem version follows the setting, becoming about 22.25px at a 768px viewport and 20px at 375px and below, while the px version stays at 18.95px. rem is recommended for font sizes.

Quick reference: clamp() for common sizes (viewport 375 to 1440px)

These are clamp() values for scaling between a 375px phone and a 1440px desktop, with a 16px root font size. The 768px and 1024px columns show how many px you actually get at those viewport widths.

Typical useSmall screen → large screenclamp()768px1024px
Notes and captions14 → 16pxclamp(0.875rem, 0.831rem + 0.1878vw, 1rem)14.74px15.22px
Body text16 → 18pxclamp(1rem, 0.956rem + 0.1878vw, 1.125rem)16.74px17.22px
Lead text and small headings16 → 24pxclamp(1rem, 0.8239rem + 0.7512vw, 1.5rem)18.95px20.88px
h324 → 32pxclamp(1.5rem, 1.3239rem + 0.7512vw, 2rem)26.95px28.88px
h228 → 40pxclamp(1.75rem, 1.4859rem + 1.1268vw, 2.5rem)32.43px35.31px
h132 → 48pxclamp(2rem, 1.6479rem + 1.5023vw, 3rem)37.90px41.75px
Large display heading (exactly 2.5 times)16 → 40pxclamp(1rem, 0.4718rem + 2.2535vw, 2.5rem)24.86px30.63px

16 → 24px and 24 → 32px both have a size difference of 8px, so they share the same 0.7512vw coefficient and only the intercept shifts by 0.5rem (8px). Remembering that the vw coefficient depends on the difference between sizes, not their ratio, makes the table easier to read. The last row, 16 → 40px, sits exactly on the “maximum within 2.5 times the minimum” line explained later.

Choosing the viewport range: what changes with 375–1440px, 320–1280px or 768–1440px?

Even for the same 16 to 24px, the sizes in between depend on which viewport range you scale across. The narrower the range, the steeper the slope, and outside the range the size stops at the minimum or maximum.

Viewport rangeclamp()375px768px1024px1440px
375–1440pxclamp(1rem, 0.8239rem + 0.7512vw, 1.5rem)16.00px18.95px20.88px24.00px
320–1280pxclamp(1rem, 0.8333rem + 0.8333vw, 1.5rem)16.46px19.73px21.87px24.00px
360–1920pxclamp(1rem, 0.8846rem + 0.5128vw, 1.5rem)16.08px18.09px19.41px21.54px
768–1440pxclamp(1rem, 0.4286rem + 1.1905vw, 1.5rem)16.00px16.00px19.05px24.00px
  • 375–1440px: uses the widths of the design mockups (the iPhone's 375px and a 1440px desktop) as they are; the most straightforward choice
  • 320–1280px: from 320px small phones such as the original iPhone SE up to 1280px, common on laptops. It reaches the maximum at 1280px, so laptop users see the large size too
  • 360–1920px: for keeping the size growing up to Full HD monitors. At 1440px it is still 21.54px and only reaches the 24px maximum at 1920px
  • 768–1440px: for keeping phones and portrait tablets at the same fixed size and making only desktops fluid

If in doubt, use the viewport widths your design mockups were made at as the minimum and maximum. The mockup sizes are reproduced exactly at both ends, and only the range in between is interpolated. When a site uses several clamp() values, keeping the same viewport range for all of them helps the ratio between headings and body text hold at every viewport width.

Working backwards: how many px does that clamp() give at each viewport width?

To find out how many px a clamp() from a template or another site's CSS gives on your screen, convert the preferred value to px and calculate. There are three steps: convert rem to px (1rem = 16px), add the vw coefficient × viewport width ÷ 100, and keep the result between the minimum and the maximum.

clamp()Minimum atMaximum at375px768px1440px
clamp(16px, 4vw, 32px)400px and below800px and above16px30.72px32px
clamp(1rem, 0.8239rem + 0.7512vw, 1.5rem)about 375px and belowabout 1440px and above16px18.95px24px
clamp(1.5rem, 1rem + 2vw, 3rem)400px and below1600px and above24px31.36px44.80px
clamp(2rem, 5vw, 4rem)640px and below1280px and above32px38.40px64px

For example, clamp(1.5rem, 1rem + 2vw, 3rem) at a 768px viewport is 16 + 2 × 768 ÷ 100 = 31.36px. The viewport width at which it reaches the minimum or maximum is “(minimum or maximum - intercept) ÷ coefficient × 100”: (24 - 16) ÷ 2 × 100 = 400px and (48 - 16) ÷ 2 × 100 = 1600px. So on a 1440px desktop this clamp() has not yet reached its 48px maximum; it is 44.80px.

If you work backwards from a clamp() rounded to four decimal places, the widths at which it reaches the minimum and maximum shift slightly, to 375.08px and 1440.04px for example, but you cannot see the difference. Paste a clamp() into the “Read clamp() (how many px?)” tab of the CSS clamp() Generator to get its value at common viewport widths and the widths at which it reaches its minimum and maximum. It supports rem, em, px, vw and calc(), and treats em the same as rem, with 1em equal to the root font size (if the parent element's font size differs from the root, the real value will differ).

Zoom and WCAG 1.4.4: vw-only sizes and “keep the maximum within 2.5 times the minimum”

WCAG 1.4.4 (Resize Text) requires that text can be enlarged to 200%. When you zoom in the browser, rem and px parts get larger, but vw is a percentage of the viewport width: zooming only makes the viewport narrower in CSS terms, so the vw part does not grow. That is why a vw-only size such as font-size: 4vw; stays at 1 times its size however far you zoom in, and the user's font size setting has no effect either.

Even with a rem part, the same problem appears if the gap between the maximum and the minimum is too large. The worst case is a large screen showing the maximum size. For example, clamp(1rem, 0.2958rem + 3.0047vw, 3rem), which scales 16 to 48px (3 times) across 375 to 1440px, is 48px at a 1440px viewport. Zoom to 500% and the viewport becomes 288px in CSS terms, so the text is 5 times the 16px minimum, or 80px. Compared with the original 48px, that is only about 1.67 times larger.

Size rangeMaximum ÷ minimumSmallest enlargement when zoomed up to 500%Rule-of-thumb result
16 → 24px1.5 timesabout 3.33 timesFine
16 → 32px2 times2.5 timesFine
16 → 40px2.5 times2.0 timesOnly just meets it
16 → 41pxabout 2.56 timesabout 1.95 timesMay fail
16 → 48px3 timesabout 1.67 timesMay fail

Chrome, Edge and Firefox can zoom up to 500%, so the smallest enlargement you get when zooming is “5 × minimum ÷ maximum”. For that to be at least 2 times, the maximum must be within 2.5 times the minimum. The CSS clamp() Generator shows a warning when a font-size maximum is more than 2.5 times the minimum, along with how many times larger the text actually gets.

Type scales: turning all your heading steps into clamp() CSS variables

Instead of choosing sizes for h1 to h6 and notes one by one, a type scale builds steps by multiplying the body text's base size by a ratio. Step n is “base × ratio to the power n”: each step up multiplies by the ratio and each step down divides by it. Using a different base and ratio on small and large screens and turning each step into a clamp() keeps headings from getting too big on phones while giving a clear difference on desktops.

With the default settings of the CSS clamp() Generator's “Type scale (heading steps)” tab (base 16px → 20px, ratio 1.2 (Minor Third) → 1.25 (Major Third), 5 steps up and 2 steps down, viewport 375 to 1440px), you get the following.

VariableSmall screensLarge screensclamp()768px
--step-539.81px61.04pxclamp(2.4883rem, 2.0213rem + 1.9927vw, 3.8147rem)47.64px
--step-433.18px48.83pxclamp(2.0736rem, 1.7292rem + 1.4695vw, 3.0518rem)38.95px
--step-327.65px39.06pxclamp(1.728rem, 1.4768rem + 1.0718vw, 2.4414rem)31.86px
--step-223.04px31.25pxclamp(1.44rem, 1.2593rem + 0.7709vw, 1.9531rem)26.07px
--step-119.20px25.00pxclamp(1.2rem, 1.0724rem + 0.5446vw, 1.5625rem)21.34px
--step-0 (body text)16.00px20.00pxclamp(1rem, 0.912rem + 0.3756vw, 1.25rem)17.48px
--step--113.33px16.00pxclamp(0.8333rem, 0.7746rem + 0.2504vw, 1rem)14.32px
--step--211.11px12.80pxclamp(0.6944rem, 0.6573rem + 0.1586vw, 0.8rem)11.73px

To use them, list these variables in :root and set font-size: var(--step-5) on h1 and var(--step-0) on body text. The higher the step, the wider the gap between small and large screens, so the vw coefficient grows too. Even the largest, --step-5, has a maximum ÷ minimum of about 1.53 times, comfortably within the 2.5-times zoom guideline.

Using clamp() for spacing, in Tailwind CSS and in CSS variables

Vertical padding on sections and the gap between cards can be made fluid with clamp() as well. To scale padding from 16 to 64px, for example, use clamp(1rem, -0.0563rem + 4.507vw, 4rem). The intercept is negative, but at viewport widths of 375px and above the vw part is large enough, and it is perfectly valid CSS. Spacing is not text, so you do not need to worry about the 2.5-times zoom guideline (the tool only shows the warning for font-size).

  • In Tailwind CSS, write an arbitrary-value class with the spaces removed: text-[clamp(1rem,0.8239rem+0.7512vw,1.5rem)], or p-[clamp(1rem,-0.0563rem+4.507vw,4rem)] for padding
  • In plain CSS, keep the spaces around + and -: if you squeeze them into 0.8239rem+0.7512vw, the browser cannot read it as an expression and ignores the whole declaration
  • If you use the same value in several places, make it a CSS variable such as --space-section: clamp(...); so that changing the viewport range later is a one-line fix
  • Choose font-size, padding, margin, gap or width under “Property” in the tool to copy that CSS and the Tailwind class directly. With a CSS variable (--fluid), no Tailwind class is shown

For width, combining it with max-width is often easier to understand. clamp() packs “minimum, preferred, maximum” into one value, so on elements that already have min-width or max-width, decide which one does the limiting; that saves confusion when you revisit the code.

How it differs from media queries, and when to use each

Before clamp(), changing text size with the viewport width usually meant switching in steps with media queries, for example “16px by default, 20px from 768px and 24px from 1280px”. With that approach, the text suddenly grows by 4px between 767px and 768px. Writing the same range as clamp(1rem, 0.8239rem + 0.7512vw, 1.5rem) gives 18.95px at 768px, with no seams between neighbouring widths.

Point of comparisonStepped media queriesSmooth clamp()
Amount of codeOne @media per stepOne line
Widths in betweenJumps at each breakpointChanges gradually in proportion to the viewport width
Best forLayout switches (one column → two columns, etc.)Continuous quantities such as font size, spacing and gap
Design reviewCheck one screen per stepCheck the mockups at both ends plus widths in between

An easy combination is media queries for clear-cut switches such as changing the number of layout columns, and clamp() for things you want to change continuously, such as font size and spacing. You can also use both, setting a different clamp() inside a media query.

Common mistakes and things to check

  • Using vw alone as the preferred value: font-size: 4vw; or clamp(16px, 4vw, 32px) does not grow with zoom or the font size setting. Add a rem intercept
  • Writing the slope as the vw coefficient: the right value is 0.7512vw (slope × 100), not 0.007512vw
  • Swapping the minimum and maximum viewport widths, or making them equal: the viewport difference becomes 0 or negative and nothing can be calculated (the tool shows “Make the maximum viewport width larger than the minimum.”)
  • Dividing by 16px when the root font size is 10px: on sites with html { font-size: 62.5%; }, divide by 10
  • Removing the spaces around + and -: plain CSS needs them. The squeezed form is only for inside Tailwind arbitrary-value classes
  • Assuming vw excludes the scrollbar: vw is 1/100 of the viewport width including the scrollbar, so on desktops it is calculated from a width slightly larger than the visible area
  • Making the maximum more than 2.5 times the minimum: there will be viewport widths where zooming does not double the text

For a clamp() you worked out by hand, it is reassuring to check how many px it gives at three widths or so, such as 375px, 768px and 1440px. On the “Make clamp()” tab of the CSS clamp() Generator, you can drag the preview's viewport width and check the graph and the table of sizes by viewport width, and “Copy URL with these settings” lets you share the same settings with your team.

FAQ

How do I calculate the vw value in clamp()?
(maximum size - minimum size) ÷ (maximum viewport width - minimum viewport width) × 100 is the vw coefficient. To scale 16 to 24px across 375 to 1440px, that is 8 ÷ 1065 × 100 ≈ 0.7512vw, and the intercept is 16 - 0.007512 × 375 ≈ 13.18px (0.8239rem), giving clamp(1rem, 0.8239rem + 0.7512vw, 1.5rem).
What does the rem part in the middle of clamp() mean?
It is the value at a viewport width of 0 (the intercept) when you draw the relationship between viewport width and size as a straight line. Text is never actually shown at that size, but writing it in rem means the text grows when the browser is zoomed or the font size setting is changed.
Should I use 375 to 1440px as the viewport range?
If your design mockups are at 375px for phones and 1440px for desktops, using those widths as they are is the most straightforward. If you want laptops to get the maximum size, use 320 to 1280px; if you want it to keep growing up to Full HD, use 360 to 1920px. Decide by where you want the maximum to be reached.
How many px is clamp(16px, 4vw, 32px) at a 768px viewport?
4 × 768 ÷ 100 = 30.72px. This clamp() is 16px at viewport widths of 400px and below and 32px at 800px and above. Note, though, that its preferred value is vw alone, so the text does not grow when the browser is zoomed.
Why is it a problem to make headings too large?
If the maximum is more than 2.5 times the minimum, there are viewport widths where zooming the browser to 500% does not double the text, which may fail WCAG 1.4.4. For 16 to 48px (3 times), it only grows about 1.67 times at a 1440px viewport.
What should I watch out for when writing clamp() in Tailwind CSS?
Inside an arbitrary-value class, remove the spaces, as in text-[clamp(1rem,0.8239rem+0.7512vw,1.5rem)]. When writing in an ordinary CSS file, + and - need spaces around them, so do not paste the contents of a Tailwind class straight into CSS.

Tools in this article

Related articles

How-to Guide11 min read

Full Moon Calendar 2027: Dates, Times and Names | The Supermoon Is January 22, the Strawberry Moon June 18–19, and There Is No Blue Moon

“When is the next full moon?” “What date is the Strawberry Moon this year?” “When can I see the supermoon?” Full moon dates shift every year, and websites name and count supermoons a little differently, so the more you search the more confusing it gets. This article lists all 12 full moons of 2027 in Universal Time (UTC) and US Eastern time, worked out with the same formulas as the Moon Phase Calendar & Birthday Moon tool (Meeus, Astronomical Algorithms, accurate to within a minute), together with their names from the Wolf Moon to the Cold Moon and the distance to the moon. It also explains, with numbers, which full moon is the biggest and smallest of 2027, why 2027 has neither a Blue Moon nor a Corn Moon, which night to look when the exact full moon falls in daytime, and how the date changes between time zones.

Read article →
How It Works11 min read

Born in January or Early February? When Your Yakudoshi Falls, Counting From Risshun, and How to Find Your Nine Star Ki Birth Star and Happo-fusagari Years | In 2027 It Is 9 Purple Fire

“I heard my unlucky years shift by one because I was born in January.” “I was born on Setsubun, so which birth star am I?” “Someone told me my happo-fusagari year and my yakudoshi fall in the same year.” Japanese unlucky years (yakudoshi) and Nine Star Ki are hardest to pin down for people born between January 1 and Setsubun. This article explains how your yakudoshi changes depending on whether a shrine starts the year on New Year’s Day or at Risshun, why Risshun moves between February 3 and 5, how to work out your birth star, which birth years are in happo-fusagari from 2026 to 2029, and why women’s honyaku at 19 and 37 always coincides with happo-fusagari. Every figure was checked with the Yakudoshi Calculator.

Read article →
How-to Guide12 min read

How to Calculate Building Coverage Ratio and Floor Area Ratio in Japan | How Big a House Fits on a 30-Tsubo Plot? Worked Examples for Front Road Width, Setback and Corner Lot Relief

A Japanese land listing may say “coverage ratio 60%, floor area ratio 200%”, but that does not tell you at a glance how big a house you can build. On top of that, the floor area ratio drops when the front road is narrow, and on roads under 4 m wide a setback takes land off the site itself. This article walks through the formulas for building coverage ratio (kenpei-ritsu) and floor area ratio (yoseki-ritsu), a chart of what fits on a 30-tsubo plot, worked examples for the front road, setbacks, corner lot and fire zone relief and the specified road relief, and how much floor area two- and three-storey houses can actually reach, all with figures from the Building Coverage & Floor Area Ratio Calculator.

Read article →