Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 139 ยท Values and Units

calc(), min(), max() and clamp()

Learn to do math in CSS with calc(), mix units, pick the smaller or larger value with min() and max(), and build fluid sizes with clamp() in a live visualizer.

CSS can do math. Instead of hard-coding a width or font size, you can write an expression and let the browser work out the answer at the moment it lays out the page. That matters because CSS often mixes values that can't be added up in advance: "the full width minus a 2rem sidebar gap", or "16px plus 2% of the screen width". Only the browser knows what those come to on a given device.

Four functions do most of this work: calc() for arithmetic, min() and max() for choosing between values, and clamp() for keeping a value inside a range. Together they replace many media queries and make fluid layouts short and readable.

calc(): arithmetic with mixed units

calc() takes an expression using +, -, * and /, and it can mix any compatible units:

CSS
.main    { width: calc(100% - 280px); }           /* room for a 280px sidebar */
.overlap { margin-top: calc(-1 * var(--gap)); }   /* negate a variable */
.third   { width: calc(100% / 3); }
.title   { font-size: calc(1rem + 1vw); }         /* rem and vw together */

The rules are few, but strict:

  • + and - need spaces around them. calc(100% - 2rem) works; calc(100%-2rem) is invalid, because -2rem reads as a negative number with no operator in front. Spaces around * and / are optional but make things easier to read.
  • * needs at least one plain number, and / needs a plain number on the right. calc(2 * 3rem) is fine; calc(3rem * 3rem) isn't a length, so it's invalid. Dividing by zero is invalid too.
  • Add like with like. You can add lengths to lengths (any units) or numbers to numbers, but not a number to a length. A bare 0 is a number, so calc(0 + 1rem) is invalid; write 0px.
  • Nest freely. Parentheses group terms, and inner calc() is allowed: calc((100% - 2rem) / 3).

A classic use: content that fills the remaining space next to a fixed-width item.

calc() for leftover space

In a real layout you'd use Flexbox or Grid for this, but you'll still meet calc() constantly for offsets, spacing and sizes based on custom properties.

min() and max(): pick one

min() takes a comma-separated list and uses the smallest value; max() uses the largest. Each argument can be a full expression, so you don't need calc() inside them.

The names confuse almost everyone at first, because they work in reverse of how they sound:

  • min() sets a maximum. width: min(100%, 600px) is "100%, but never more than 600px".
  • max() sets a minimum. font-size: max(1rem, 2vw) is "2vw, but never less than 1rem".
CSS
.container {
  width: min(100% - 2rem, 70rem);   /* full width with 1rem gutters, capped at 70rem */
  margin-inline: auto;
}

.hero { padding-block: max(2rem, 8vh); }   /* never cramped on short screens */

That .container rule is one of the most useful lines in CSS: it gives a centered page column with side gutters on small screens and a maximum width on large ones, with no media query at all.

A container in one line with min()

clamp(): a value with limits

clamp(MIN, PREFERRED, MAX) returns the preferred value, but never less than MIN and never more than MAX. It's exactly the same as max(MIN, min(PREFERRED, MAX)), written more readably.

CSS
h1 { font-size: clamp(1.75rem, 1rem + 3vw, 3rem); }

Read it as: "Try 1rem + 3vw. If that's below 1.75rem, use 1.75rem. If it's above 3rem, use 3rem."

The preferred value is usually a fluid expression that changes with the screen, like 1rem + 3vw. The rem part keeps the size responsive to the user's font settings and zoom, while the vw part makes it grow with the window. (Sizing text in vw alone causes accessibility problems, as Viewport Units explains.) If MIN is larger than MAX, MIN wins.

Live clamp() visualizer

The box below has an adjustable width. Its heading uses clamp() with container query units: cqi is 1% of the box's width, just as vw is 1% of the window's (you'll study them in Container Queries). Drag the width slider and watch which part of clamp() is in charge. Then change the three values.

clamp() visualizer

The shaded band is the allowed range and the red line is the size actually used. At narrow widths the heading sticks to MIN, in the middle it grows smoothly with the box, and at wide widths it stops at MAX. Try a MIN that's bigger than MAX and see that MIN wins.

Working out the preferred value

To make text grow from exactly 20px at a 400px viewport to 40px at 1200px, you need a straight line through those two points:

  1. Slope: (40 โˆ’ 20) / (1200 โˆ’ 400) = 0.025, which is 2.5vw.
  2. Starting offset: 20 โˆ’ 0.025 ร— 400 = 10px, which is 0.625rem.
  3. Result: clamp(1.25rem, 0.625rem + 2.5vw, 2.5rem).

You don't have to do this by hand every time; many online calculators produce these values. Fluid Typography builds a whole type scale this way.

Common patterns

CSS
/* Fluid spacing that tracks the window, within limits */
.section { padding-block: clamp(2rem, 6vw, 5rem); }

/* Grid columns that are at least 220px, or the whole row on tiny screens */
.cards { grid-template-columns: repeat(auto-fill, minmax(min(220px, 100%), 1fr)); }

/* Width that's 50% on desktop but never narrower than 18rem */
.dialog { width: max(18rem, 50%); max-width: 100%; }

/* Sticky header offset from a variable */
.anchor-target { scroll-margin-top: calc(var(--header-height) + 1rem); }

The minmax(min(220px, 100%), 1fr) pattern prevents a classic overflow bug: without the inner min(), a 220px minimum would overflow a container narrower than 220px. You'll use it in Grid Tracks.

More math functions

Newer CSS adds more math: round(), mod() and rem() for stepped values, abs() and sign(), and trigonometry with sin(), cos(), tan(), atan2() and friends, plus pow(), sqrt() and hypot(). The trigonometric and exponential functions are supported in all major browsers since 2023, and round(), mod() and rem() since 2024. abs() and sign() arrived in Chrome last, so check support before relying on them. You'll rarely need these, but they're handy for things like placing items around a circle.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is width: calc(100%-20px) invalid?

    Without spaces, -20px is read as a negative number, leaving no operator. Write calc(100% - 20px).
  2. 2. What does width: min(90%, 500px) mean?

    min() picks the smaller value, so it acts as an upper limit.
  3. 3. What is clamp(1rem, 4vw, 2rem) on a 320px-wide viewport, with a 16px root font size?

    4vw of 320px is 12.8px, which is less than 1rem (16px), so the minimum is used.
  4. 4. Which calc() is valid?

    Length times length isn't allowed, a bare 0 is a number that can't be added to a length, and division by zero is invalid.
  5. 5. Why mix rem into the preferred value, as in 1rem + 3vw, instead of using 4vw alone?

    Pure vw values barely change when the user zooms; a rem part scales with zoom and font preferences.

Summary

  • calc() does arithmetic and can mix units; put spaces around + and -, and only multiply or divide by plain numbers.
  • min() picks the smallest value, so it works as a maximum; max() picks the largest, so it works as a minimum.
  • width: min(100% - 2rem, 70rem) makes a responsive page column in one line.
  • clamp(MIN, PREFERRED, MAX) keeps a fluid value between two limits; combine rem and vw (or cqi) for the preferred value.
  • Newer functions like round() and the trigonometric functions exist for special cases.

Next: Custom Properties (CSS Variables) โ€” storing values once and reusing them everywhere.