Skip to main content
🎨 CSS as a design system·Module B6 · Lesson 3
TaskBuild a hero block using clamp() for both padding and font-size. .hero { padding: clamp(2rem, 8vw, 6rem); background: #18181B; color: white; } .hero h1 { font-size: clamp(2rem, 6vw + 1rem, 5rem); margin: 0; }.

clamp() everywhere: fluid sizing without media queries

75 XP6 min
Theory

clamp() recap from B4 — now applied broadly

h1 { font-size: clamp(2rem, 5vw + 1rem, 4rem); }
.section { padding: clamp(2rem, 8vw, 6rem); }
.container { width: clamp(320px, 90%, 1200px); }
.gap { gap: clamp(0.5rem, 2vw, 1.5rem); }

Anything that's a LENGTH — font-size, padding, margin, width, gap — can be a clamp(). The middle value is the "preferred" growth curve; the min and max bound it.

The recipe

For "X on small screens, Y on large":

clamp(X, X + (Y-X) * vw_scale, Y)

Or use a calculator (juniorconsult.dev/fluid-calculator) and never math again.

When NOT to use clamp

  • When the value MUST be a specific pixel value (borders, focus rings).
  • When the value MUST snap to integer pixels (font-size sometimes — sub-pixel rendering inconsistencies on some platforms).
  • When the value depends on PARENT width (use @container instead — clamp is viewport-based).

Mix with @container

Inside an @container block, you can still use clamp:

@container (min-width: 400px) {
  .card { padding: clamp(16px, 3cqw, 32px); }   /* cqw = container-query-width */
}

cqw is "container query width unit" — 1cqw = 1% of the container's width. Combine clamp + cqw for the most flexible fluid sizing.

🔒

Sign up to start coding

Theory is open to everyone. The interactive editor, live preview, and check are unlocked with a 7-day free trial — card required, cancel anytime.

Sign up — free trial →

First 15 lessons in each track are free. No card needed for those.

PreviousNext lesson →

Get one Python or web tip a day — by email

Short, hand-written, no spam. Unsubscribe in one click.