CSS Reference ยท Units and Values
CSS math functions overview
Overview of CSS math functions: calc(), min(), max(), clamp(), round(), mod(), trigonometry, pow() and abs(), with a guide to choosing the right one.
CSS can do arithmetic. Math functions compute a value at the time the browser resolves styles, which lets you mix units (100% - 2rem), pick the smaller or larger of two sizes, clamp a size between limits, or round and rotate values using trigonometry. Anywhere CSS accepts a length, number, angle, time or percentage, a math function that returns that type is usually accepted too.
This is a concept page: it explains how the family fits together and points to a page for each function.
.card { width: min(100% - 2rem, 60ch); font-size: clamp(1rem, 0.9rem + 1vw, 1.5rem); margin-inline: calc((100% - 60ch) / 2); }
Try it
The functions at a glance
| Function | What it does |
|---|---|
calc() |
Adds, subtracts, multiplies and divides, mixing units. |
min() |
Returns the smallest of its arguments. |
max() |
Returns the largest of its arguments. |
clamp() |
clamp(MIN, VALUE, MAX): a value held between limits. |
round(), mod(), rem() |
Stepped rounding, modulus and remainder. |
sin(), cos(), tan(), asin(), acos(), atan(), atan2() |
Trigonometry for angles and positions. |
pow(), sqrt(), hypot(), log(), exp() |
Powers, roots and logarithms. |
abs(), sign() |
Absolute value and the sign of a number. |
Rules that apply to all of them
- Whitespace around
+and-is required insidecalc():calc(1rem+2px)is invalid,calc(1rem + 2px)is valid.*and/do not require it. - Types must make sense. You can add two lengths or a length and a percentage, but not a length and a plain number. In a multiplication at least one side must be a plain number; in a division the right side must be a plain number.
- Nesting is fine.
min(),max()andclamp()accept calculations directly without writingcalc()inside them. - Percentages resolve against the property.
width: calc(50% - 1rem)uses the containing block's width; the same expression onmargin-leftalso uses width. - Custom properties.
calc(var(--gap) * 2)works, and typed custom properties allow animation of the result. - Avoid dividing by zero. The result is an infinite or undefined value whose handling depends on the property, so guard it.
Usage notes
- Use
min()for upper limits,max()for lower limits, andclamp()when you have both. - Fluid typography is a classic
clamp()job: a readable minimum, a viewport-based middle, a sensible maximum. - Prefer a media query or container query when the layout needs to change, not just scale.
- Keep accessibility in mind: text sized only in viewport units may not respond to the user's zoom or font settings. Include a
remterm, as in1rem + 1vw.
Browser support
calc(), min(), max() and clamp() work in all current browsers. round(), mod(), rem(), trigonometric and exponential functions are newer; check a compatibility table before relying on them without a fallback.
Related
calc()โ the foundationclamp()โ fluid valuesvar()- Lesson: Advanced Math Functions
- Lesson: Units: px, em, rem and More