ShaharAmir
← Back to Blog
CSS1 min read

CSS clamp() for Fluid Typography

One line of CSS for perfectly responsive font sizes. No media queries needed

S
Shahar Amir

The Magic Formula

css
1
font-size: clamp(min, preferred, max);

Real Example

css
123456789
h1 {
/* Min 24px, scales with viewport, max 48px */
font-size: clamp(1.5rem, 4vw + 1rem, 3rem);
}
p {
/* Readable on all screens */
font-size: clamp(1rem, 2.5vw, 1.25rem);
}

How It Works

  • min: Smallest the value can be
  • preferred: The ideal size (usually viewport-based)
  • max: Largest the value can be

The browser picks whichever value is in the middle.

Beyond Typography

css
12345678910
.container {
/* Fluid width */
width: clamp(300px, 80%, 1200px);
/* Fluid padding */
padding: clamp(1rem, 5vw, 3rem);
/* Fluid gap */
gap: clamp(0.5rem, 2vw, 2rem);
}

The Formula Explained

css
123456
/* 4vw + 1rem means:
- On 400px screen: 16px + 16px = 32px
- On 800px screen: 32px + 16px = 48px
- But clamped between min and max!
*/
font-size: clamp(1.5rem, 4vw + 1rem, 3rem);

Pro Tip

Use this tool to generate clamp values: utopia.fyi/type/calculator

No more breakpoint soup. One line. Done.

#typography#responsive#modern-css#fluid

Stay Updated 📬

Get the latest tips and tutorials delivered to your inbox. No spam, unsubscribe anytime.