Prejsť na hlavný obsah
← 🎨 CSS as a design system·Modul B6 · Lekcia 3
ÚlohaBuild 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() všade: plynulé určovanie veľkosti bez mediálnych dotazov

75 XP6 min
Teória

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.

🔒

Zaregistruj sa a začni programovať

Teória je otvorená pre každého. Editor, živý náhľad a kontrola sa odomknú so 7-dňovou bezplatnou skúškou — karta potrebná, zruš kedykoľvek.

Registrácia — bezplatná skúška →

Prvých 10 lekcií v každej trase je zdarma. Karta nie je potrebná.

← PredchádzajúcaĎalšia lekcia →

Jeden tip k Pythonu alebo webu denne — e-mailom

Krátke, ručne písané, žiadny spam. Odhlásiť jedným klikom.