Tutorials CSS calc(), min(), max()
Ch 49 / 60
Chapter 49 Intermediate 9 min read CSS

CSS calc(), min(), max()

CSS math functions let you perform arithmetic directly in your stylesheets — no JavaScript, no preprocessor needed. calc() is the workhorse: it mixes different units (px, %, rem, vw) in a single expression. min() returns the smaller of two values, capping at an upper bound. max() returns the larger, setting a lower floor. Together they solve layout problems that were previously impossible in pure CSS — and clamp() (covered in chapter 42) is just a shorthand for max(MIN, min(MAX, VALUE)).

What you'll learn
calc() — arithmetic with mixed units
Operators: +, -, *, / rules
min() — upper bound / ceiling
max() — lower bound / floor
Nesting functions together
Real-world layout patterns

calc() — Mixed-Unit Arithmetic

calc() evaluates a mathematical expression at render time. Its superpower is mixing units from different dimensions — something no single unit can do. It supports four operators: +, -, *, /.

width :  calc(100% - 2rem);
property
where the result goes
calc()
math function wrapper
100%
relative to parent
-
operator (must have spaces)
2rem
different unit — fixed
calc() in action — bars showing computed widths
calc(100% - 2rem)
full width minus 2rem
calc(50% + 20px)
50% + 20px
calc(100% / 3)
one third
calc(100% / 3 * 2)
two thirds
calc(2rem * 3)
6rem (unitless multiplier)
CSS — calc() syntax and rules
/* ── calc() basics ── */
.el { width:   calc(100% - 2rem); }     /* % minus rem — impossible any other way */
.el { width:   calc(50% + 20px); }     /* half + a fixed gutter */
.el { height:  calc(100vh - 64px); }   /* full viewport minus navbar */
.el { padding: calc(1rem + 2vw); }    /* fixed base + fluid viewport */

/* ── Operator rules ── */
.el { width: calc(100% - 2rem); }    /* ✓ + and - MUST have whitespace around them */
.el { width: calc(100%-2rem); }      /* ✗ INVALID — no space around - */
.el { width: calc(100% * 2); }       /* ✓ * can have or not have space */
.el { width: calc(100% / 3); }       /* ✓ divide by unitless number */
.el { width: calc(10px / 5px); }    /* ✗ INVALID — can't divide by unit */
.el { width: calc(100% * 50%); }    /* ✗ INVALID — can't multiply two units */

/* ── Nesting calc() ── */
.el { width: calc(calc(100% / 3) - 1rem); }  /* nested — inner calc optional */
.el { width: calc(100% / 3 - 1rem); }        /* same result — simpler */

/* ── calc() with custom properties ── */
:root { --nav-height: 64px; --gap: 1rem; }
.main { height: calc(100dvh - var(--nav-height)); }
.col  { width:  calc(50% - var(--gap) / 2); }

/* ── Math operator precedence (standard) ── */
.el { width: calc(10px + 4px * 3); }  /* = 10px + 12px = 22px */
.el { width: calc((10px + 4px) * 3); } /* = 14px * 3  = 42px */

min() — Upper Bound / Ceiling

min(a, b) returns the smaller of its arguments. Because it picks the minimum, it acts as a ceiling — the result is never larger than the smallest value you provide. Use it when you want something to grow freely but cap at a maximum.

min(a, b) — picks smaller
width: min(80%, 600px)
narrow screen
80%<600px → 80%
wide screen
600px<80% → 600px

min() = upper bound. Never exceeds the smallest arg.

max(a, b) — picks larger
padding: max(1rem, 3vw)
narrow screen
1rem>3vw → 1rem
wide screen
3vw>1rem → 3vw

max() = lower bound. Never below the largest arg.

CSS — min() and max() patterns
/* ── min() — picks the SMALLER value (upper bound / ceiling) ── */

/* Container: full width on mobile, max 1200px on desktop */
.container { width:       min(90%, 1200px); margin-inline: auto; }

/* Image: fill container but never bigger than natural size */
.img       { width:       min(100%, 400px); }

/* Sidebar: 25% of viewport or 300px, whichever is smaller */
.sidebar   { width:       min(25vw, 300px); }

/* Font: scales with viewport but caps at readable size */
h1         { font-size:  min(8vw, 3.5rem); }

/* ── max() — picks the LARGER value (lower bound / floor) ── */

/* Padding: at least 1rem, grows proportionally on wide screens */
.section   { padding:    max(1rem, 3vw); }

/* Min touch target: always at least 44px */
.btn       { min-height: max(44px, 2.75rem); }

/* Font: never smaller than 1rem regardless of viewport */
body       { font-size:  max(1rem, 1.5vw); }

/* Gap: at least 1rem, grows on wide screens */
.grid      { gap:        max(1rem, 2vw); }

/* ── Multiple arguments (3+) ── */
.el { width: min(100%, 800px, 90vw); }  /* picks smallest of all three */
.el { padding: max(1rem, 2vw, 12px); }  /* picks largest of all three */

Nesting Functions Together

The real power appears when you combine calc(), min(), max() and custom properties. They can nest freely and mix without extra wrappers in modern CSS:

clamp() = max + min combined
clamp(MIN, PREF, MAX) is exactly equivalent to max(MIN, min(MAX, PREF)). Understanding this equivalence helps you see how they compose — and lets you use min() and max() for single-boundary constraints when you don't need both.
CSS — nesting and combining math functions
/* ── clamp() equivalence ── */
h1 { font-size: clamp(1.5rem, 4vw, 3rem); }
h1 { font-size: max(1.5rem, min(3rem, 4vw)); }  /* exactly equivalent */

/* ── calc() inside min() / max() ── */
.container {
  width: min(100% - 2rem, 1200px);  /* shorthand for calc(100%-2rem) or 1200px */
  margin-inline: auto;
}

/* Note: In CSS Level 4, + - inside min()/max() is implicit calc()
   so min(100% - 2rem, 1200px) works without writing calc() */

/* ── max() inside calc() ── */
.sidebar-layout {
  display: grid;
  /* sidebar at least 200px, but no more than 20% */
  grid-template-columns: max(200px, 20%) 1fr;
}

/* ── With custom properties ── */
:root { --nav-h: 64px; --sidebar-w: 280px; }

.page   { min-height: calc(100dvh - var(--nav-h)); }
.main   { width: calc(100% - var(--sidebar-w) - 1.5rem); }
.avatar { width: clamp(32px, calc(2rem + 1vw), 56px); }

/* ── Arithmetic with CSS variables ── */
:root { --cols: 3; --gap: 1rem; }
.col {
  width: calc((100% - var(--gap) * (var(--cols) - 1)) / var(--cols));
  /* = (100% - 2rem) / 3  →  perfect 3-col grid with gutters */
}

Real-World Layout Patterns

1. Fluid Container — The Classic Pattern

The most-used single-line responsive pattern. The element fills its parent but never exceeds the max-width, and always has side gutters:

width: min(100% - 2rem, 1200px) — container always has 1rem each side
min(100% - 2rem, 1200px)

2. Sidebar Layout — Fixed + Fluid

display:grid; grid-template-columns: max(200px,20%) 1fr
Sidebar
max(200px,20%)
Main Content (1fr)

3. Full-Page Layout — Subtract Navbar

min-height: calc(100dvh - 64px)
Navbar — 64px
Page content — min-height: calc(100dvh - 64px)
CSS — real-world calc/min/max patterns
/* ── 1. Fluid container (the must-know pattern) ── */
.container {
  width:          min(100% - 2rem, 1200px);
  margin-inline:  auto;
}
/* ↑ On mobile: 100% - 2rem (1rem each side)
      On wide: capped at 1200px and centred */

/* ── 2. Subtract navbar height ── */
.page-content { min-height: calc(100dvh - 64px); }

/* ── 3. Sidebar layout ── */
.layout {
  display:               grid;
  grid-template-columns: max(200px, 20%) 1fr;
  /* sidebar: at least 200px OR 20% of container, whichever is larger */
  gap:                   max(1rem, 2vw);
}

/* ── 4. Dynamic column grid with gutters ── */
:root { --cols: 3; --gap: 1rem; }
.col { width: calc((100% - var(--gap) * (var(--cols) - 1)) / var(--cols)); }

/* ── 5. Sticky sidebar that doesn't overflow ── */
.sticky-sidebar {
  position:  sticky;
  top:       calc(var(--nav-h) + 1rem);
  max-height:calc(100vh - var(--nav-h) - 2rem);
  overflow-y:auto;
}

/* ── 6. Aspect ratio image with calc ── */
.video-wrap {
  position:    relative;
  padding-top: calc(9 / 16 * 100%);  /* 56.25% — 16:9 ratio */
}
/* Modern: use aspect-ratio: 16/9 instead */

/* ── 7. Button width: at least content, at most 300px ── */
.btn {
  width:     min(100%, max(content-fit, 160px));  /* future syntax */
  max-width: 300px;
  min-height:max(44px, 2.75rem);  /* accessibility touch target */
}

/* ── 8. Fluid spacing without media queries ── */
.section  { padding-block: max(2rem, min(8vw, 5rem)); }
.prose    { margin-block:  max(.75rem, 1.5vw); }
.icon     { width:         max(24px, min(3vw, 48px)); }

Function Reference & Rules

Function What it returns Role Best for
calc(expr) Result of arithmetic expression Mixed-unit math Subtracting fixed from fluid, gutter math
min(a, b, …) Smallest of all arguments Upper bound / ceiling Max-width containers, capping font or image size
max(a, b, …) Largest of all arguments Lower bound / floor Minimum padding, touch targets, minimum font size
clamp(min, pref, max) pref, clamped between min and max Both bounds Fluid typography, fluid spacing
calc() operator rules
Operator Rule Example
+ Both operands must have units (or both unitless) calc(100% + 2rem) ✓
- Same as + · must have whitespace on both sides calc(100% - 2rem) ✓   calc(100%-2rem) ✗
* One operand must be unitless calc(2rem * 3) ✓   calc(2rem * 3px) ✗
/ Right operand must be unitless calc(100% / 3) ✓   calc(100% / 3px) ✗
Free Resource
CSS3 Cheat Sheet — Math Functions & Layout Patterns
Complete reference covering calc(), min(), max(), clamp() and responsive layout patterns.
Download PDF

Try It Yourself

All three math functions in one editor — resize the preview pane to see how calc(), min() and max() respond:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 49

Questions & Answers
Single CSS units are all relative to one reference: % is relative to the parent, rem is relative to the root font size, vw is relative to the viewport width, px is absolute. You can never express a value that combines two different reference frames with a single unit.

calc() bridges these worlds. The canonical example:
width: calc(100% - 2rem)
— "Fill the parent completely, but subtract 2rem (a fixed amount relative to the font size)"
— No single unit can express this — you'd need two references simultaneously

Other examples that require calc():
— height: calc(100vh - 64px) — viewport minus navbar
— width: calc(100% / 3 - 1rem) — one third minus a gutter
— padding: calc(1rem + 2vw) — fixed base plus viewport-relative growth
The four operators and their rules:

+ and -: both operands must have units (or both be unitless). MUST have whitespace on both sides — this is the most common mistake.
— ✓ calc(100% - 2rem)
— ✗ calc(100%-2rem) — browser may parse the - as a negative number

*: exactly one operand must be a unitless number
— ✓ calc(2rem * 3)
— ✗ calc(2rem * 3px) — can't multiply two units

/: the right operand must be unitless
— ✓ calc(100% / 3)
— ✗ calc(100% / 3px)

Standard operator precedence applies: * and / before + and -. Use parentheses to override.
The naming is counterintuitive until you understand their role:

min(a, b) returns the SMALLER value — so it acts as an upper bound (ceiling). The result can never be LARGER than the smaller argument. Use when you want something to grow freely but cap at a maximum.
— width: min(90%, 1200px) — grows to 90% on narrow screens, caps at 1200px on wide ones
— Think: "I want AT MOST 1200px"

max(a, b) returns the LARGER value — so it acts as a lower bound (floor). The result can never be SMALLER than the larger argument. Use when you want to guarantee a minimum.
— padding: max(1rem, 3vw) — grows with viewport but never below 1rem
— Think: "I want AT LEAST 1rem"

Memory trick: min() sets a maximum. max() sets a minimum. They do the opposite of what their names suggest about the result.
This is the canonical fluid container pattern — one of the most useful CSS expressions:

width: min(100% - 2rem, 1200px)

Breaking it down:
— 100% - 2rem: fill the parent minus 1rem on each side (creates gutters)
— 1200px: the maximum allowed width
— min(): picks whichever is smaller

Result on a 375px mobile: 100% - 2rem = ~343px, which is less than 1200px → container fills with side gutters
Result on a 1400px desktop: 100% - 2rem = 1368px, which exceeds 1200px → caps at 1200px

Combined with margin-inline: auto, it centres the container on wide screens while filling with side gutters on narrow screens — zero media queries needed. Previously this required max-width: 1200px; padding-inline: 1rem; but min() does it in one line.
clamp(MIN, PREF, MAX) is exactly equivalent to max(MIN, min(MAX, PREF)).

Working it out with clamp(1rem, 4vw, 3rem):
1. min(3rem, 4vw): picks the smaller — caps the preferred value at 3rem (ceiling)
2. max(1rem, result): picks the larger — ensures the result is at least 1rem (floor)

So clamp is just a convenient shorthand for "apply a ceiling, then apply a floor." You could always write it the long way with min() and max() nested — clamp() just makes the intent clearer.

When to use which:
— min(): only need a ceiling
— max(): only need a floor
— clamp(): need both — most common for fluid typography and spacing
Multiple Choice Questions
Question 1 of 5
Which CSS declaration is INVALID due to calc() operator rules?
Question 2 of 5
What does width: min(90%, 1200px) produce on a 1440px wide screen?
Question 3 of 5
What does padding: max(1rem, 3vw) guarantee?
Question 4 of 5
What value does calc(10px + 4px * 3) resolve to?
Question 5 of 5
Which expression is equivalent to clamp(1rem, 4vw, 3rem)?
Chapter Summary
  • calc(expression): evaluates arithmetic at render time. Superpower: mix units from different dimensions in one value. calc(100% - 2rem) = impossible with any single unit.
  • Operator rules: + and - require whitespace on both sides. * requires one unitless operand. / requires the right operand to be unitless. Standard math precedence applies.
  • min(a, b): returns the smaller — acts as an upper bound / ceiling. Use when something should grow but never exceed a maximum: width: min(90%, 1200px).
  • max(a, b): returns the larger — acts as a lower bound / floor. Use to guarantee a minimum: padding: max(1rem, 3vw).
  • clamp(MIN, PREF, MAX) = max(MIN, min(MAX, PREF)) — both bounds at once. Covered in ch42.
  • Fluid container: width: min(100% - 2rem, 1200px); margin-inline: auto — the single most useful CSS expression for responsive layouts.
  • Nesting: functions compose freely. calc() inside min(), max() inside calc(), custom properties inside calc() — all valid.
  • Touch target floor: min-height: max(44px, 2.75rem) ensures both pixel and rem requirements are met.
  • Dynamic columns: width: calc((100% - (cols - 1) * gap) / cols) creates perfect equal-width columns with any gutter.
~9 min read
Intermediate level
Chapter 49 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel