CSS Clamp() Calculator
Below you’ll find a CSS clamp() calculator that lets you visually create responsive values you can copy and paste directly into your CSS.
The clamp() function is used to define a value that scales fluidly between a minimum and maximum size, based on the viewport. It’s most commonly used for responsive font sizes, spacing, widths, and other layout values - without relying on media queries.
In its simplest form, clamp() looks like this:
clamp(minimum, preferred, maximum)
- Minimum – the smallest value allowed
- Preferred – a fluid value, usually based on viewport units
- Maximum – the largest value allowed
The browser automatically picks the best value depending on screen size, staying within the defined limits.
How the visual calculator helps
Instead of manually calculating viewport formulas, this calculator lets you:
- Set minimum and maximum sizes
- Choose viewport ranges
- See the result update live as you resize
- Instantly copy the generated clamp() CSS
It’s perfect for responsive typography, padding, margins, gaps, and more - with predictable, smooth scaling across all screen sizes.
CSS Clamp() Calculator
How the Magic Happens
1. The Math: Linear Interpolation
Most people think clamp() is just a random guess. In reality, this calculator uses Linear Interpolation. It maps your desired font size to the viewport width using the slope-intercept form:
- m (The Slope): This determines the
vw(Viewport Width) unit. It tells the browser exactly how many pixels to add for every 100px of screen width. - b (The Intercept): This is the "Base" size in
rem. It ensures that even at a theoretical screen width of 0px, your font has a starting point.
2. Accessibility: The "REM" Rule
A common mistake is using px inside a clamp() function. If you use clamp(16px, 2vw, 32px), the text will not grow if a user increases their browser's default font size for better visibility.
By converting your inputs into rem, our calculator ensures:
User Choice: If a user sets their base font to 24px (instead of 16px), your entire fluid scale shifts upward proportionally.
WCAG Compliance: Your site remains accessible to those with visual impairments.
3. Understanding the "Fluid Zone"
The graph above highlights the Fluid Zone.
Below Min Viewport: The
minimumvalue takes over. Your design stays locked and readable on the smallest phones.The Fluid Zone: This is where the
preferredvalue is active. The font scales smoothly as you resize the window.Above Max Viewport: The
maximumvalue kicks in. This prevents your headers from becoming comically large on ultra-wide monitors.
4. Fluid Line-Height: The "Pro" Secret
As fonts get larger, they naturally appear to have more space between lines. To keep your typography looking "tight" and professional on desktop, our calculator allows for Inverse Scaling.
Pro Tip: Set your Min Viewport Line-Height to
1.5and your Max Viewport Line-Height to1.2. This creates a sophisticated "tightening" effect as the screen gets wider.
Common use cases for clamp()
The clamp() function is incredibly versatile and works well anywhere you want values to scale smoothly without breaking your layout. Some common use cases include:
Responsive font sizes Create fluid typography that grows with the viewport while staying readable on small screens.
Padding and margins Adjust spacing dynamically so layouts feel balanced on both mobile and large displays.
Gaps and grid spacing Scale spacing between flex or grid items without hard breakpoints.
Container widths Control how wide elements grow while preventing them from becoming too small or too large.
UI elements Responsively size buttons, icons, and cards with consistent limits.
Frequently Asked Questions
What does the CSS clamp() function do?
clamp() restricts a value to a range between a defined minimum and maximum, with a preferred fluid value in between.
It takes three arguments: clamp(minimum, preferred, maximum).
The browser uses the preferred value when it falls within the range, clamps to the minimum when the preferred would go below it, and clamps to the maximum when it would exceed it.
It is most commonly used for responsive font sizes and spacing that scale smoothly with the viewport without needing media queries.
What is the difference between clamp(), min(), and max() in CSS?
min() returns the smallest of two or more values, effectively setting a ceiling.
max() returns the largest, effectively setting a floor.
clamp(minimum, preferred, maximum) combines both - it is equivalent to writing max(minimum, min(preferred, maximum)).
Use clamp() when you need both a floor and a ceiling around a fluid value, and min() or max() when you only need one constraint.
Why should I use rem instead of px inside clamp()?
Using px inside clamp() means your values are fixed in absolute pixels and will not respond to a user's browser font size preference.
If a user increases their default font size for accessibility, a px-based clamp will ignore that setting entirely.
Using rem units ensures your fluid values scale proportionally with the user's chosen base font size, keeping your layout accessible and compliant with WCAG guidelines on text resizing.
How do I calculate the preferred value inside clamp()?
The preferred value is typically a viewport-relative expression calculated using linear interpolation between your minimum and maximum sizes across your chosen viewport range.
The formula is: slope = (maxSize - minSize) / (maxViewport - minViewport), and intercept = minSize - slope * minViewport.
The preferred value then becomes calc(intercept + slope * 100vw).
This is exactly what this calculator computes for you automatically - enter your min size, max size, and viewport range and the correct expression is generated instantly.
Can I use clamp() for things other than font sizes?
Yes - clamp() works for any CSS property that accepts a length, percentage, or number value.
Common use cases beyond font sizes include padding and margins (so spacing scales with the viewport), gap values in flexbox and grid layouts, container widths to prevent elements becoming too narrow or too wide, and sizing UI elements like buttons, icons, and cards.
Anywhere you would previously have written multiple @media query breakpoints to step a value up or down, clamp() can often replace them with a single smooth fluid expression.
What is the fluid zone in a clamp() function?
The fluid zone is the viewport width range within which the preferred (middle) value of clamp() is active and scaling.
Below the minimum viewport width, the value locks to the minimum and stops shrinking.
Above the maximum viewport width, it locks to the maximum and stops growing.
In between - the fluid zone - the value scales smoothly and linearly with the viewport width.
Setting this range correctly is the key to fluid typography that feels natural on every screen size.
How do I use clamp() for responsive typography?
Set a minimum font size appropriate for small phones, a maximum for large desktop screens, and choose viewport breakpoints that represent the range over which you want the text to scale.
Feed those four values into the calculator and copy the generated font-size: clamp(...); declaration directly into your CSS.
For best accessibility, use rem units so the scale responds to user font preferences.
You can optionally pair it with a fluid line-height: clamp(...); that tightens slightly as the font grows larger, which keeps large headings from appearing too loose on wide screens.
Does clamp() work in all browsers?
Yes. clamp() has full support across all modern browsers - Chrome 79+, Firefox 75+, Safari 13.1+, and Edge 79+.
Global browser support is above 96%. For the small percentage of older browsers that do not support it, the safest approach is to include a plain fallback value on the line before the clamp() declaration, for example font-size: 1.25rem; font-size: clamp(...); - browsers that understand clamp() will use it, others will fall back to the fixed value above.
What is fluid line-height and why does it matter?
Fluid line-height means your line-height value also scales with the viewport using clamp(), rather than staying fixed.
As font sizes increase on wider screens, a fixed line-height ratio can make headings appear too loose and airy.
By setting the minimum viewport line-height slightly higher than the maximum (for example 1.5 on mobile down to 1.2 on desktop), you get an inverse scaling effect - the leading tightens naturally as type grows, giving large headings a more professional, print-like appearance.
Can clamp() replace media queries entirely?
For fluid sizing and typography, clamp() can often replace the repetitive step-based @media queries that previously handled incremental size changes.
Instead of defining a font size at three or four breakpoints, a single clamp() expression covers the entire range smoothly.
However, clamp() is not a full replacement for media queries - layout changes like switching from a single-column to a multi-column grid, showing or hiding elements, or changing component structure still need media queries.
Think of clamp() as handling the gradual scaling within a layout, while media queries handle the structural shifts.
