Math Functions: calc(), min(), max(), clamp(), round() and Trigonometry
Doing math in CSS: calc() and its type rules, min(), max() and clamp() for responsive limits, round(), mod() and rem(), abs() and sign(), trigonometry, exponents and constants, typed division, progress(), random(), and accessible font-size math.
kvi5hnuCode Sprout team member9 min readOn this page
CSS values don't have to be fixed. Math functions compute a value from other values when the page is rendered, mixing units that can't be combined in advance: a percentage minus a length in rem, a viewport size plus a fixed minimum, an angle from a ratio. They work anywhere a length, percentage, number, integer, angle, time, frequency or resolution is allowed.
calc()
calc() evaluates an arithmetic expression with +, -, *, / and parentheses:
.sidebar { width: calc(100% - 18rem); } /* everything but a fixed column */
.card { width: calc((100% - 2 * 1rem) / 3); } /* three columns with two 1rem gaps */
.hero { min-height: calc(100svh - 4rem); } /* the screen minus a header */
The key ability is mixing units: 100% - 18rem can't be computed until the browser knows the container's width and the root font size, so CSS does it for you during layout.
Syntax rules
+and-need spaces around them.calc(100% -2rem)is invalid, because-2remreads as a negative number with no operator.*and/don't need spaces, but use them for readability.- Parentheses group as in ordinary math, and
calc()can be nested:calc(100% - calc(2rem + 4px)). Nestedcalc()is the same as plain parentheses. - Inside other math functions,
calc()isn't needed:min(100%, 60rem - 2rem)is valid.
Type rules
calc() checks that the units make sense:
| Operation | Rule | Example |
|---|---|---|
+, - | both sides of the same type, such as both lengths (percentages count if the property accepts them) | 100% - 2rem |
* | the final result must be a valid type | 2 * 1.5rem |
/ | dividing by a number keeps the type; dividing by the same type gives a number | 30rem / 4, 100vw / 1px |
calc(5rem + 2) is invalid: a length plus a number has no meaning. A result of the wrong type also makes the whole declaration invalid: width: calc(10px * 3px) would be an area, not a length. Intermediate steps may have unusual units, as long as the result is right: calc(10px * 3px / 1px) is 30px.
Typed division, dividing a length by a length to get a plain number, became Baseline in October 2026. It turns lengths into numbers for properties that need them:
.box { z-index: calc(100vw / 1px); } /* viewport width as a number, e.g. 1280 */
.bar { --done: 120px; --total: 480px; scale: calc(var(--done) / var(--total)) 1; } /* length ÷ length = 0.25 */
Division by zero no longer invalidates the declaration: the result is infinite and gets clamped to the property's allowed range. Avoid relying on it.
min(), max() and clamp()
These pick a value from a list:
min(a, b, ...)uses the smallest value. It sets a maximum:width: min(100%, 60rem)is never wider than 60rem, and fits smaller containers.max(a, b, ...)uses the largest value. It sets a minimum:padding: max(1rem, 4vw)never gets below 1rem.clamp(min, preferred, max)uses the preferred value, kept between a minimum and a maximum. It's the same asmax(min, min(preferred, max)).
.page { width: min(100% - 2rem, 70rem); margin-inline: auto; } /* centered, with side margins on phones */
.section { padding-block: clamp(2rem, 6vw, 5rem); }
h1 { font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3.5rem); }
The names confuse people at first: min() gives a maximum size, and max() gives a minimum size. Read min(100%, 60rem) as "the smaller of the two".

Read each row from left to right: min() stops growing at its cap, max() refuses to shrink below its floor, and clamp() follows the 50% in the middle column but holds at its floor and its cap on either side.
If clamp()'s maximum is smaller than its minimum, the minimum wins. Arguments can be any number of values and expressions, in min() and max(), in any order.
body { font-family: system-ui, sans-serif; }
.bar { height: 1.75rem; margin-bottom: 0.5rem; color: white; font-size: 0.85rem;
display: flex; align-items: center; padding-inline: 0.5rem; border-radius: 4px; box-sizing: border-box; }
.a { width: min(100%, 30rem); background: #8a3b12; }
.b { width: max(12rem, 40%); background: #2f7d4f; }
.c { width: clamp(10rem, 60%, 36rem); background: #1d4ed8; }
Font sizes and accessibility
Limits on font sizes can block users' text zooming. WCAG asks that text can be enlarged to 200%:
- Put
remin every part of a font-size expression, especially the minimum and maximum, so the user's default size still matters:clamp(1rem, 0.8rem + 1vw, 1.5rem), notclamp(16px, 1vw, 24px). - Keep the maximum at most about 2.5 times the minimum, so that zooming still enlarges the text enough.
Article 77 shows how to calculate fluid type scales.
Stepped values: round(), mod() and rem()
.grid { width: round(down, 100%, 12rem); } /* the largest multiple of 12rem that fits */
.thumb { height: round(37.4px, 1px); } /* 37px: snap to whole pixels */
.stripe { margin-left: mod(var(--offset), 2rem); }
round(strategy?, value, step)roundsvalueto a multiple ofstep. The strategy isnearest(the default),up,downorto-zero. With only a number, the step defaults to 1:round(2.6)is 3.mod(a, b)is the remainder with the sign ofb:mod(-7px, 5px)is3px.rem(a, b)is the remainder with the sign ofa:rem(-7px, 5px)is-2px. Don't confuse the functionrem()with the unitrem.
They're useful for snapping sizes to a grid or a baseline, and for repeating patterns.

On the left, one formula produces a whole type scale from a step number. On the right, the bars never show part of a segment: round(down, …) drops whatever is left over after the last full 60px.
Signs: abs() and sign()
abs(x)is the absolute value.sign(x)is-1,0or1. Combined with multiplication, it can turn a value into a switch:calc(sign(var(--count)) * 1rem)is1remwhen the count is positive and0when it's zero.
Trigonometry and exponents
CSS has the usual functions:
| Functions | Return |
|---|---|
sin(), cos(), tan() | a number, from an angle or a number of radians |
asin(), acos(), atan(), atan2(y, x) | an angle |
pow(x, y), sqrt(x), hypot(a, b, ...), log(x, base?), exp(x) | a number, or for hypot() the input's type |
Trigonometry places elements on circles without JavaScript:
.dial { position: relative; width: 14rem; height: 14rem; margin: 1rem auto; border: 2px dashed #c9b37e; border-radius: 50%; }
.dot {
--angle: calc(var(--i) * 45deg);
position: absolute;
left: calc(50% + 6rem * cos(var(--angle)) - 1rem);
top: calc(50% + 6rem * sin(var(--angle)) - 1rem);
width: 2rem; height: 2rem; border-radius: 50%;
background: #8a3b12; color: white; font: 700 0.8rem/2rem system-ui, sans-serif; text-align: center;
}
hypot() gives distances, and atan2() an angle from two lengths, for example to rotate an element toward a point. pow() builds modular type scales: font-size: calc(1rem * pow(1.25, var(--step))).
Constants
Math functions accept a few named constants: pi, e, infinity, -infinity and NaN. calc(2 * pi * 1rem) is the circumference of a 1rem circle. infinity is clamped to the largest value the browser supports, which is a common way to say "as large as possible", as in border-radius: calc(infinity * 1px) for a pill shape.
Newer functions
-
progress(value, start, end)returns where a value lies between two others, as a number from 0 to 1. Values outside the range are clamped. It's Baseline since September 2026:css.title { opacity: progress(100cqi, 20rem, 40rem); } /* fade in as the container grows */ -
random(min, max)picks a random value when the style is computed:rotate: random(-5deg, 5deg). It's in Chrome 156 and Safari 26.2, and Firefox 161 is about to ship it. -
calc-size()allows math on intrinsic sizes such asautoandfit-content, mostly for animating toheight: auto. It's in Chrome only (article 108). -
sibling-index()andsibling-count()give an element's position among its siblings as a number (article 30).
Math functions and custom properties
Math functions are most powerful with custom properties (article 27), which can hold numbers to compute with:
.stack > * { --gap: 1; margin-block-start: calc(var(--gap) * 1rem); }
.stack.loose > * { --gap: 2; }
To turn a plain number into a length, multiply by one unit: calc(var(--n) * 1px). You can't paste a unit onto a number with text, as in var(--n)px.
What's current
calc(),min(),max()andclamp()have worked everywhere for years.round(),mod()andrem()are Baseline since May 2024,abs()andsign()since June 2025, and the trigonometric and exponential functions since 2023.- Typed division (
100vw / 1px) is Baseline since October 2026, andprogress()since September 2026. random()is in Chrome and Safari, with Firefox shipping it in November 2026.calc-size()is Chrome only.
Common questions
Why is calc(100% -2rem) invalid?
+ and - must have whitespace on both sides. Without it, -2rem is read as a negative number directly after 100%, with no operator between them.
Why does min() set a maximum?
It picks the smaller value, so the result can never be bigger than any of its arguments. width: min(100%, 60rem) can't exceed 60rem.
Can I use calc() in media queries?
Yes: @media (width >= calc(40rem + 2px)) works. Note that rem in media queries is the user's default size, not your root font size (article 24).
How do I convert a unitless custom property to pixels?
Multiply: calc(var(--size) * 1px). Going the other way, from a length to a number, works with typed division: calc(var(--len) / 1px).
Can clamp() take its arguments in any order?
No. It's always minimum, preferred, maximum. Only min() and max() don't care about order.
Summary
calc()mixes units in arithmetic.+and-need spaces, and the final type must suit the property.min()caps a value,max()sets a floor, andclamp(min, preferred, max)does both.- Use
remin every part of font-size expressions, so users' settings and zoom still work. round(),mod()andrem()snap and repeat values.abs()andsign()handle signs.- Trigonometric and exponential functions, plus
pi,eandinfinity, allow geometric layouts and type scales. - Typed division turns lengths into numbers, and multiplying by
1pxturns numbers into lengths. progress()is Baseline,random()nearly so, andcalc-size()Chrome only.
Practice questions
Write a width for a content column that is at most 65rem, fills smaller screens, and always leaves 1rem of space on each side.
Answer
.content { width: min(100% - 2rem, 65rem); margin-inline: auto; }
What's the computed value of padding: max(1rem, 3vw) in a 400px-wide viewport, with a 16px root font size? And in a 1200px viewport?
Answer
At 400px, 3vw is 12px, so the larger value, 16px, is used. At 1200px, 3vw is 36px, which is larger, so 36px.
Why is font-size: clamp(12px, 2vw, 20px) a poor choice, and what would you write instead?
Answer
Every part is in pixels or viewport units, so the user's default font size has no effect, and at small widths the text can be just 12px. Use rem in all three parts: clamp(1rem, 0.85rem + 0.75vw, 1.25rem).
A custom property --cols holds a number. Write a width that divides the container into that many equal columns with 1rem gaps between them.
Answer
width: calc((100% - (var(--cols) - 1) * 1rem) / var(--cols));
Previous: Viewport Units: vw, vh, and the Small, Large and Dynamic Viewport Units
Next: Custom Properties: Variables, Fallbacks, Scope and Changing Them at Runtime

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.