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 10 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.