How to Make Fluid Font Sizes with CSS clamp(): Formula, Ready Values and Glassmorphism
Updated: November 6, 2026 · AracMerkezi
For text that grows and shrinks with the screen width, people used to write many media queries. Today a single CSS function, clamp(), solves this in one line in most cases. The real difficulty is not the function itself but calculating the "preferred" value in the middle. This guide derives, step by step, a formula that lets you say "make it this size on a 400 px screen and that size on a 1200 px screen", gives three worked examples and checks the results at different widths. A short section on glassmorphism comes at the very end.
How does clamp() work?
clamp() takes three values: clamp(minimum, preferred, maximum). The browser calculates the preferred value but never lets the result go outside the minimum and maximum. Its logical equivalent is max(minimum, min(preferred, maximum)).
h1 { font-size: clamp(1.5rem, 1rem + 3vw, 3rem); }
In this example the heading is at least 1.5 rem (24 px) when the screen narrows and at most 3 rem (48 px) when it widens; in between it flows with the window width. A 16 px base is assumed.
Why isn't vw alone enough?
If you write only font-size: 4vw, text becomes unreadably small on a small screen and huge on a very wide one. Also, when the user enlarges the browser's font size, a value based only on vw does not respond to it. That is why a rem + vw combination is recommended for the preferred value: 1rem + 3vw. The rem part stays responsive to the user's setting, and the vw part provides the flow.
The formula: a line through two points
The goal is for the size to be S1 at screen width W1 and S2 at W2. The preferred value is the line through these two points:
- Slope = (S2 − S1) / (W2 − W1)
- vw coefficient = Slope × 100
- Constant (px) = S1 − Slope × W1; divide by 16 for rem.
- Result:
clamp(S1, constant + vw coefficient·vw, S2); don't forget to convert S1 and S2 to rem.
Example 1: Body text, 400 px → 1200 px, 18 px → 32 px
- Slope = (32 − 18) / (1200 − 400) = 0.0175 → 1.75 vw
- Constant = 18 − 0.0175 × 400 = 11 px = 0.6875 rem
- Result:
clamp(1.125rem, 0.6875rem + 1.75vw, 2rem)
Verification (calculated with Python): at 320 px the raw value is 16.6 px, so it is pinned to 18 px; 24.44 px at 768 px; 28.92 px at 1024 px; 32 px at 1200 px; at 1440 px the raw value is 36.2 px, so it stays at 32 px.
Example 2: Heading, 320 px → 1280 px, 24 px → 48 px
- Slope = 24 / 960 = 0.025 → 2.5 vw
- Constant = 24 − 0.025 × 320 = 16 px = 1 rem
- Result:
clamp(1.5rem, 1rem + 2.5vw, 3rem)
| Screen width | Calculated size |
|---|---|
| 320 px | 24 px |
| 400 px | 26 px |
| 768 px | 35.2 px |
| 1024 px | 41.6 px |
| 1200 px | 46 px |
| 1440 px | 48 px |
The size reaching the upper limit at 1280 px is the expected result: (48 − 16) / 0.025 = 1280.
Example 3: Spacing, 320 px → 1280 px, 16 px → 64 px
Slope = 48 / 960 = 0.05 → 5 vw; constant = 16 − 0.05 × 320 = 0. Since the constant is zero the formula simplifies: padding: clamp(1rem, 5vw, 4rem);. It is 38.4 px at 768 px and 51.2 px at 1024 px.
Where else is it used besides text?
- Spacing:
padding: clamp(1rem, 4vw, 3rem); - Width:
width: clamp(280px, 90%, 720px); - Line length: To keep a text column readable.
Accessibility
- Don't pick a minimum that is too small; for body text, dropping below 16 px (1 rem) is generally not recommended.
- Use rem instead of px so that user settings are respected.
- Browser zoom (Ctrl +) narrows the viewport width in CSS pixels. Zooming to 200% on a 1440 px screen makes the width 720 CSS px, and the heading in Example 2 drops to 34 px; but on screen it occupies 34 × 2 = 68 physical pixels, so it still gets bigger. Verify by testing the WCAG expectation that text can be enlarged up to 200%.
- Check text and background contrast separately: Color Contrast Checker.
Browser support
clamp() is supported by all modern browsers. If you target very old browsers, write a fixed font-size first and then add the clamp() line; a browser that does not support it uses the first line.
Common mistakes
- Using only vw in the preferred value and ignoring the browser's font-size setting.
- Choosing a minimum that is too low for body text.
- Not setting a maximum and getting huge headings on wide monitors.
- Mixing px and rem and getting unexpected sizes; forgetting to divide by 16 when converting the constant to rem.
- Scaling heading, body and spacing all with the same formula; each element has different needs.
- Mixing up the positions of the min and max values; the first value is always the smallest.
Test suggestion
Try the design at 320 px, 768 px and 1440 px widths, and check readability by raising the browser's text size to 200%. If the result reads comfortably on both small and large screens, the formula is working.
Short section: the glassmorphism effect
The "frosted glass" look is achieved with a semi-transparent background and blurring of the content behind it: .card { background: rgba(255,255,255,.18); backdrop-filter: blur(12px); border: 1px solid rgba(255,255,255,.3); border-radius: 16px; }. backdrop-filter does not work in some older browsers, so the background should be left opaque enough. Heavy use of blur can affect performance on low-powered devices. Always check the contrast between the text color and the glass surface. For details see How to Make Glassmorphism with CSS.
Tools
If calculating the formula by hand is tedious, the CSS clamp() Calculator generates it for you. To convert between px and rem use the PX REM EM Converter, and to understand the difference use the guide PX, REM and EM Differences. For ready glass-effect code try the Glassmorphism CSS Generator.
Frequently asked questions
What is the clamp() function for?
It keeps a value between a minimum and a maximum limit so that you get a fluid size that follows the screen width.
Why isn't using only vw enough?
Text gets too small on small screens, too big on large ones, and it does not respond to the user's browser font-size setting; combining it with rem is better.
How do I calculate a clamp() value?
Slope = (S2 − S1) / (W2 − W1); the vw coefficient is 100 times the slope; constant = S1 − slope × W1. For 18 px at 400 px and 32 px at 1200 px the result is clamp(1.125rem, 0.6875rem + 1.75vw, 2rem).
Tools mentioned in this guide
More guides
- How to Make Glassmorphism (Frosted Glass Effect) With CSS – How do you make a frosted glass effect with CSS? backdrop-filter, semi-transparent background, border and shadow values, plus support and contrast tips.
- How to Convert HEX to RGB Color Codes: Step-by-Step Examples – How to convert a HEX color code to RGB and back: hexadecimal basics, short and 8-digit HEX, darkening math and verified JavaScript and Python snippets.
- How to Choose a Color Palette for a Website: Building a Full Palette From One Color – How to choose a website color palette: build tints, accents and neutrals from one HEX color, with the 60-30-10 rule, contrast values and CSS variables.
- What Is Color Contrast Ratio? A Step-by-Step WCAG AA and AAA Calculation with Example Colors – How is color contrast ratio calculated? WCAG AA and AAA thresholds (4.5:1, 3:1, 7:1), the luminance formula, worked color examples and tips.
- What Is the Difference Between PX, REM and EM? CSS Units, a Conversion Table and Which to Use When – What is the difference between px, rem and em in CSS, and which to use when? The px-to-rem formula, an em compounding example and a 16 px conversion table.
This article is for general information only and is not legal, financial or medical advice. We are not an official institution.