日本語

CSS clamp() for Responsive Font Size Based on Screen Width

​​CSS​Accessibility,HTML,Playwright,Responsive

Using CSS clamp() for font-size lets you scale text smoothly with the screen width without writing any media queries. On narrow screens the text never gets smaller than the minimum, and on wide screens it never gets larger than the maximum.

css
p {
  font-size: clamp(1rem, 0.75rem + 1.25vw, 1.5rem);
}

With this one line, the text is 16px at a 320px screen width and 24px at 960px. In between, it grows at a constant rate as the width increases.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened a test page with Playwright, changed the screen width, and read the font-size value with getComputedStyle. I reproduced changes to the browser's default font size with Page.setFontSizes from the Chrome DevTools Protocol. I did not test Firefox, Safari, or display under browser zoom. The browser version support information comes from the compatibility data (@mdn/browser-compat-data 8.1.8).

clamp() picks the value that fits within the range from three values

clamp(minimum, preferred, maximum) returns the middle preferred value, limited to the range between the minimum and the maximum. The CSS Values and Units Level 4 specification states that clamp(MIN, VAL, MAX) represents exactly the same value as max(MIN, min(VAL, MAX)).

If you put vw, a unit equal to 1% of the viewport width, in the middle value, the text size changes with the screen width. As the specification allows, you can use + and * inside each argument without writing calc().

I measured the font-size of a paragraph with clamp(1rem, 0.75rem + 1.25vw, 1.5rem) in the test environment while changing the screen width.

Screen width font-size
280px 16px
320px 16px
400px 17px
480px 18px
640px 20px
800px 22px
960px 24px
1280px 24px
1600px 24px
Graph with screen width (200 to 1600px) on the horizontal axis and font-size on the vertical axis. The line stays flat at 16px up to 320px, rises in a straight line from 320px to 960px to reach 24px, and stays flat at 24px for widths above 960px. (The labels are in Japanese.)
font-size of clamp(1rem, 0.75rem + 1.25vw, 1.5rem) measured at each screen width

Below 320px, the text stops at the minimum of 16px. Above 960px, it stops at the maximum of 24px. In between, the text grew by 2px for every 160px increase in screen width.

The following image shows the top-left corner of the same page opened at widths of 360px, 640px, and 960px. The heading also uses a clamp() value created with the calculation method described below.

Three top-left corners of the same page opened at widths of 360px, 640px, and 960px, stacked vertically. From top to bottom, the heading and body text get larger. The heading reads "Cat Notebook" and the body text reads "The text size changes." (The labels are in Japanese.) The heading is 32.0px, 37.3px, and 43.4px, and the body text is 16.5px, 20.0px, and 24.0px.
The same page opened with only the screen width changed

Calculate the middle expression from the widths and sizes you want

The middle expression is a straight line that passes through two points: the font size should be X px when the screen width is A px, and Y px when the width is B px. You calculate it as follows.

  1. Slope = (Y − X) ÷ (B − A)
  2. Number before vw = slope × 100
  3. Intercept = X − A × slope (px)
  4. Number before rem = intercept ÷ 16

In the example above, A = 320, X = 16, B = 960, and Y = 24. The slope is 8 ÷ 640 = 0.0125, so the value is 1.25vw. The intercept is 16 − 320 × 0.0125 = 12px, so the value is 0.75rem. You write the minimum and maximum in rem as X and Y, which gives clamp(1rem, 0.75rem + 1.25vw, 1.5rem).

The same method works when the numbers don't divide evenly. Suppose you want a heading to be 2rem (32px) at 360px and 3rem (48px) at 1200px. The slope is 16 ÷ 840 = 0.019047…, and the intercept is 25.142…px. Rounding to four decimal places gives the following expression.

css
h1 {
  font-size: clamp(2rem, 1.5714rem + 1.9048vw, 3rem);
}

In the test environment, the heading was 32px at 360px and 48px at 1200px. At the in-between widths of 570px, 780px, and 990px, it was 35.9998px, 39.9998px, and 43.9999px. The calculated values are 36px, 40px, and 44px. The difference of about 0.0002px comes from rounding the decimals in 1.5714rem and 1.9048vw.

Note that the "÷ 16" in step 4 assumes the browser's default font size is 16px. As the next section shows, when the default font size changes, the pixel values of the parts written in rem also change.

Mixing rem into the middle expression makes the browser's font size setting take effect

If you set the text size with vw alone, the text does not get larger when you change the browser's default font size. rem is a unit based on the font size of the root element, so it changes along with the browser's default font size.

I compared the following three approaches in the test environment with default font sizes of 16px and 24px.

Default font size, screen width 2.5vw clamp(16px, 12px + 1.25vw, 24px) clamp(1rem, 0.75rem + 1.25vw, 1.5rem)
16px, 375px 9.375px 16.6875px 16.6875px
16px, 800px 20px 22px 22px
16px, 1280px 32px 24px 24px
24px, 375px 9.375px 16.6875px 24px
24px, 800px 20px 22px 28px
24px, 1280px 32px 24px 34px

With 2.5vw alone, the text shrinks to 9.375px at a 375px width, and changing the default font size to 24px made no difference. The clamp() written in px also produced exactly the same numbers when I changed the default font size.

Only the clamp() written entirely in rem grew with the default font size. When the default is 24px, the minimum and maximum become 24px and 36px, and the 0.75rem intercept becomes 18px. As a result, at a 1280px width, the text was 18 + 16 = 34px.

The explanation of WCAG Success Criterion 1.4.4 (Resize Text) says that web content must not prevent the browser from enlarging text. When you scale text with the screen width, write the minimum, maximum, and intercept in rem. As the table above shows, the text then grows with the browser's font size setting.

If the minimum is greater than the maximum, the minimum wins

If you swap the minimum and maximum, clamp() returns the minimum. The specification also says that when the minimum and maximum conflict, the minimum takes priority.

In the test environment, clamp(24px, 1vw, 16px) at an 800px width gave a font-size of 24px. It was neither the middle 1vw (8px) nor the maximum of 16px. If the text size does not change as expected, check that the arguments are in the order minimum, preferred, maximum.

Supported in major browsers

According to the same data as the compatibility table on the MDN clamp() page (@mdn/browser-compat-data), clamp() is supported in Chrome 79, Edge 79, Firefox 75, Safari 13.1, Safari on iOS 13.4, and Chrome on Android 79 and later.

​Posted by 86