CSS Clamp Generator
Generate fluid clamp() declarations for any CSS property.
Enter your viewport breakpoints and size targets to get a precise,
copy-pasteable CSS output with no math required.
Show calculation breakdown
- Min clamped value
- 1rem
- Max clamped value
- 2.5rem
- Slope
- 0.015 px per px of viewport
- Y-intercept
- 11.2px
- Preferred value
- 0.7rem + 1.5vw
What Is CSS clamp() and Why Does It Matter?
The CSS clamp() function lets you set a value that scales fluidly between a minimum
and maximum based on the current viewport width. Instead of writing multiple breakpoint-specific
media queries to manage different screen sizes, a single clamp() declaration handles
the full range continuously and proportionally.
The function signature is: clamp(minimum, preferred, maximum). The preferred value
is a viewport-relative expression that combines vw units with a rem
offset, causing the value to grow linearly as the screen widens, up to the defined maximum.
The Math Behind the Generator
The preferred value inside clamp() is calculated using linear interpolation between
two known data points: the target size at the minimum viewport width, and the target size at
the maximum viewport width.
The formulas used are:
slope = (maxSize - minSize) / (maxViewport - minViewport)
intercept = minSize - (slope x minViewport)
preferred = intercept + (slope x 100vw)
Both the slope and intercept are calculated in pixels, then converted to rem and
vw for the CSS output. The result is a single declaration that scales perfectly
across every screen width between your two breakpoints, with hard floor and ceiling values
enforced by the min and max arguments.
Why Fluid Typography Reduces Layout Shift
Cumulative Layout Shift (CLS) is a Core Web Vital used by Google as a direct ranking signal.
When font sizes or spacing values change abruptly at media query breakpoints, the resulting
reflow causes measurable CLS penalties that hurt both user experience and search rankings.
Fluid values derived from clamp() eliminate these jumps entirely by making changes
continuous rather than stepped.
Beyond CLS, fluid typography improves cross-device readability. A heading that is too large on a small phone or too small on a widescreen monitor forces users to scroll, zoom, or disengage. Proportional scaling removes that friction with no JavaScript overhead.
clamp() vs. Media Queries: When to Use Each
clamp() is the right tool when a value should scale proportionally across a
continuous viewport range. Common use cases include:
- Body text and heading font sizes
- Section padding and vertical rhythm spacing
- Component gap and margin values
- Line heights relative to viewport
Media queries remain the right tool for structural layout changes: switching from a
single-column to a two-column grid, hiding or showing elements, or changing component
orientation. Use both in combination. Let clamp() handle proportional
scaling and media queries handle layout mode shifts.
Common Viewport Breakpoint Reference
These standard viewport widths serve as a reliable baseline for most projects. Use your smallest supported device as the min viewport and your content max-width (or 1920px) as the max.
| Breakpoint Name | Width (px) | Common Devices |
|---|---|---|
| XS — Small Mobile | 320 | iPhone SE, older Android |
| Mobile | 375 — 390 | iPhone 14, Galaxy S22 |
| Large Mobile | 430 | iPhone 14 Plus, Pixel 7 |
| Tablet | 768 | iPad portrait |
| Desktop | 1280 — 1440 | Standard laptop and monitor |
| Wide Desktop | 1920 | Full HD monitor |
Using rem vs. px for Font Sizes
This generator defaults to rem units and that is intentional. Rem values are
relative to the user's browser base font size (typically 16px), which means they respect
accessibility settings. If a user increases their browser font size for readability, rem-based
values scale with that preference. Pixel values are absolute and override it.
For font-size and line-height, always use rem. For
spacing properties like gap, padding, and margin,
either unit is acceptable, though rem remains the more accessible choice.
This aligns with WCAG 2.1 Success Criterion 1.4.4 (Resize Text), which requires text to be resizable up to 200% without loss of content or functionality. Absolute pixel font sizes can fail this criterion.
Tips for Using This Generator
- Set min viewport to the smallest device you support (320px or 375px is standard) and max viewport to 1920px or your layout's max-width.
- Use the Quick Presets as a starting point and adjust the size values to match your specific design system scale.
- The CSS Property dropdown covers common properties, but any CSS property that accepts a length value works. Select "custom..." to enter any property name.
- Paste the output directly into your CSS file. No further calculation is needed. The value is production-ready as generated.
- Use the "Show calculation breakdown" panel to understand the slope and intercept values. This is useful for debugging or verifying the output against your design specs.