What does clamp() do for fluid sizes?

clamp(minimum, preferred, maximum) gives a value that follows the preferred value but never goes below the minimum or above the maximum. With a preferred value built from vw units, which are percentages of the viewport width, a font size or spacing grows smoothly with the screen instead of jumping at breakpoints. This is often called fluid typography.

How to use the generator

  1. Enter the size you want on small screens and the size you want on large ones, in px or rem.
  2. Set the viewport widths where the size should start and stop growing.
  3. Choose whether the result uses rem or px, and change the root font size if your site doesn't use the browser's default of 16px.
  4. Copy the clamp() value and use it for font-size, padding, margin, gap or any other length.

Move the preview slider to see the size at any viewport width, and check the table for the sizes on common screens.

How the value is calculated

The size grows in a straight line from the minimum size at the minimum viewport width to the maximum size at the maximum width:

  • slope = (max size − min size) / (max viewport width − min viewport width)
  • base = min size − slope × min viewport width
  • preferred value = base + slope × 100 vw

Below the minimum width the size stays at its minimum, and above the maximum width at its maximum. Numbers are rounded to 4 decimals. If the large-screen size is smaller than the small-screen one, the value shrinks as the screen grows, and the limits are swapped so clamp() still works.

rem or px?

With rem, the minimum, maximum and base of the value follow the visitor's font size settings, so text still gets bigger when someone raises the default font size in their browser. That's why rem is the default output. The vw part always depends on the screen width alone.

Examples

  • 16px to 32px between 360px and 1280px: clamp(1rem, 0.6087rem + 1.7391vw, 2rem)
  • The same in px: clamp(16px, 9.7391px + 1.7391vw, 32px)
  • 1rem to 1.5rem between 360px and 1440px: clamp(1rem, 0.8333rem + 0.7407vw, 1.5rem)

Features

  • Sizes in px or rem, output in rem or px
  • Any viewport range and root font size
  • Growing and shrinking sizes
  • Live preview at any viewport width
  • Sizes at common screen widths
  • Runs in your browser