# calc(), clamp(), min() & max()

> CSS math functions let you combine units and set fluid bounds without media queries. calc() does the math, clamp() caps a value between a floor and ceiling, and min()/max() pick the smaller or larger candidate.

Subject: CSS | Lesson 8 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/calc-clamp-and-functions
Previous: [CSS Units: px, rem, em, %, viewport](https://codewithanimation.in/css/units-and-values.md) | Next: [Overflow, Outline & Display](https://codewithanimation.in/css/overflow-and-display.md)

## Four math functions for values that compute themselves

CSS has four math functions that turn a property value into a small calculation. **`calc()`** evaluates one expression and can mix units that nothing else can: `calc(100% - 2rem)` subtracts a fixed gutter from a fluid width. This single capability solves layout problems that used to need wrappers or JavaScript.

**`clamp(MIN, PREFERRED, MAX)`** takes three comma-separated values. It returns `PREFERRED`, but never below `MIN` and never above `MAX`. **`min()`** and **`max()`** take a comma-separated list and return the smallest or largest candidate. Together these four let a value respond to viewport or container size with hard bounds and no media query.

## Fluid values replace most media queries

Before `clamp()`, responsive type meant a breakpoint ladder: `1rem` at mobile, `1.25rem` at 768px, `1.5rem` at 1024px. The steps are visible and the rules multiply. With `clamp(1rem, 2vw + 1rem, 2rem)` the font-size slides smoothly between two sane limits and you write one declaration instead of three.

`min()` and `max()` do the same for dimensions. A reading column of `width: min(100% - 2rem, 65ch)` is always full width minus padding on small screens and never wider than a comfortable line length on large ones. One value, two constraints, zero breakpoints. This is the modern responsive default.

## The rules each function follows

**`calc()`** accepts `+`, `-`, `*`, `/`. The non-obvious rule: `+` and `-` MUST be surrounded by whitespace on both sides. `calc(100% - 10px)` works; `calc(100%-10px)` is invalid and the browser silently drops the whole declaration. For `*` and `/`, at least one operand must be a unitless number: `calc(2rem * 3)` is valid, `calc(2rem * 3rem)` is not. Nesting is allowed and the inner `calc()` acts as parentheses: `calc(calc(100% - 2rem) / 2)`.

**`clamp(MIN, PREFERRED, MAX)`** evaluates `PREFERRED` first, then clamps it. All three values must resolve to the same type (all lengths, all percentages, or all numbers). The most readable fluid-type recipe is `clamp(1rem, 2vw + 1rem, 2rem)`: the `2vw + 1rem` preferred value scales with the viewport but is anchored to a readable base, and the floor and ceiling stop it from getting too small or too large.

**`min(a, b, ...)`** returns the smallest value, **`max(a, b, ...)`** returns the largest. Each comma-separated value is computed independently and the function picks one. `min(100% - 2rem, 60ch)` is a self-contained responsive column: whichever is smaller wins, so the column shrinks on narrow screens and caps at `60ch` on wide ones.

All four compose. `clamp()`, `min()`, and `max()` can contain `calc()` inside any argument, and they work with `var()` custom properties. A spacing scale defined as `--gap: clamp(1rem, 2vw, 2rem)` then used as `calc(var(--gap) * 2)` gives you a fluid, bounded system with no breakpoints at all.

## Common mistake: Missing whitespace breaks calc() with no error

`calc(100%-2rem)` looks reasonable but is invalid: the `-` needs a space on both sides, so it must be `calc(100% - 2rem)`. The browser does not warn you, it just ignores the declaration and falls back to the previous value. If a `calc()` value is mysteriously not applying, the whitespace around `+` and `-` is the first thing to check. Multiplication and division do not have this requirement, but adding the space anyway keeps the rule simple: always space your `+` and `-`.

## Tip: Reach for clamp() before a media query

When you are about to write a media query just to change a font-size or a gap, try `clamp()` first. `font-size: clamp(1.125rem, 1rem + 1vw, 1.75rem)` gives smooth scaling between two readable limits for free. Reserve media queries for changes that are truly discrete, like switching a layout from one column to two. Fluid values handle the in-between, media queries handle the jumps.

## Try it: Try it

Drag the viewport width and watch `clamp(1rem, 5vw, 3rem)` scale the heading, then hit the floor and ceiling. See `calc()` subtract a fixed gutter from a fluid width.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/calc-clamp-and-functions

## Check yourself

### Question 1

Which of these `calc()` declarations is INVALID and will be ignored by the browser?

- A. calc(100% - 2rem)
- B. calc(2rem * 3)
- C. calc(100%-2rem)
- D. calc(var(--gap) / 2)

Correct answer: C

The whitespace rule applies only to `+` and `-`, but it is the #1 calc() bug. `calc(100%-2rem)` is invalid because the minus has no surrounding spaces; it must be `calc(100% - 2rem)`. The browser drops the declaration with no error, so the failure is silent.

- Why not A: This is valid: the `-` has whitespace on both sides, which is required for `+` and `-` in calc().
- Why not B: This is valid: one operand is a unitless number, which is the rule for `*` and `/`. The result is 6rem.
- Why not D: This is valid: division by a unitless number is allowed, and calc() works inside var() custom properties.

### Question 2

Given `font-size: clamp(1rem, 5vw, 3rem)` and a viewport width of 400px (root font-size 16px), what is the computed font-size?

- A. 1rem (16px)
- B. 3rem (48px)
- C. 20px (1.25rem)
- D. 5vw (20px) but clamped to 16px

Correct answer: C

clamp(MIN, PREFERRED, MAX) returns PREFERRED whenever it sits inside the range. 5vw of a 400px viewport is 20px (1.25rem). That is above the 1rem (16px) floor and below the 3rem (48px) ceiling, so clamp returns the preferred value: 20px. The floor and ceiling only take over at the extremes.

- Why not A: 16px is the MIN floor. The floor only takes over when the preferred value falls below it. 5vw at 400px is 20px, which is above 16px, so the floor is not used.
- Why not B: 48px is the MAX ceiling. The ceiling only takes over when the preferred value exceeds it. 5vw at 400px is 20px, which is below 48px, so the ceiling is not used.
- Why not D: Clamping to 16px only happens when the preferred value is below the floor. 20px is above 16px, so no clamping occurs and the value stays 20px.

### Question 3

You want a reading column that is always full width minus 2rem of padding, but never wider than 60 characters. Which single declaration does it?

- A. width: calc(100% - 2rem, 60ch);
- B. width: min(100% - 2rem, 60ch);
- C. width: clamp(100% - 2rem, 60ch, 100%);
- D. width: max(100% - 2rem, 60ch);

Correct answer: B

min() is designed for 'whichever is smaller wins.' `min(100% - 2rem, 60ch)` shrinks with the viewport on small screens (where 100% - 2rem is smaller) and caps at 60ch on large screens (where 60ch is smaller). One value, two constraints, no media query.

- Why not A: calc() takes ONE expression, not a comma-separated list. The comma list with a pick is the job of min() or max().
- Why not C: clamp takes (MIN, PREFERRED, MAX). Here 60ch would be the preferred value and 100% the max, which does not express 'never wider than 60ch' as a hard cap. min() is the right tool for 'pick the smaller of these two'.
- Why not D: max() returns the LARGER value. On a wide screen that would pick 100% - 2rem and ignore the 60ch limit entirely, which is the opposite of what you want.

## Remember this

- `calc()` mixes units: `calc(100% - 2rem)`. `+` and `-` MUST have whitespace on both sides or the declaration is silently dropped.
- `clamp(MIN, PREFERRED, MAX)` returns PREFERRED, clamped to the range. `clamp(1rem, 2vw + 1rem, 2rem)` is the fluid-type recipe.
- `min(a, b)` returns the smaller, `max(a, b)` returns the larger. Use `min(100% - 2rem, 60ch)` for a bounded responsive column.
- All four compose with `var()`: `calc(var(--gap) * 2)` inside `clamp()` gives a fully fluid, bounded spacing system with no breakpoints.
