- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Math Functions
CSS Foundations
CSS Math Functions
Build fluid dimensions and dependable constraints by combining calc(), min(), max(), and clamp() with relative CSS units. It shows how calc(), min(), max(), and clamp() express fluid relationships while preserving readable minimum and maximum limits.
CSS can do arithmetic.
calc()adds and subtracts values in different units,min()andmax()pick the smaller or larger of several, andclamp()keeps a value between a floor and a ceiling. Together they replace a surprising number of media queries.
Try it
A heading whose size follows the viewport, but never gets smaller than 1.5rem or bigger than 3rem. Run it, then drag the preview pane narrower and wider.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>clamp() heading</title>
<style>
h1 { font-size: clamp(1.5rem, 1rem + 2.5vw, 3rem); }
body { font-family: system-ui, sans-serif; padding: 24px; }
</style>
</head>
<body>
<h1>Fluid heading, no media queries</h1>
<p>Resize the window. The heading scales with it but stays between 1.5rem and 3rem.</p>
</body>
</html>calc()
calc() lets you mix units the browser only knows how to resolve at render time. "Full width minus the 250px sidebar" is impossible to write as a single value, and trivial as calc(100% - 250px).
css
.content { width: calc(100% - 250px); }
.hero { min-height: calc(100vh - 64px); } /* viewport minus the header */
.grid-item { width: calc((100% - 2 * 16px) / 3); } /* three columns with two 16px gaps */
.icon { top: calc(50% - 12px); } /* centre a 24px icon vertically */You can add, subtract, multiply and divide. Multiplication and division need one side to be a plain number: calc(100% / 3) is fine, calc(100% / 3px) is not, because "percent per pixel" is not a unit CSS has.
min() and max()
Each takes a comma-separated list and uses whichever value is smallest (or largest) once resolved. They read backwards from how people expect, so it helps to say them out loud:
css
/* "as wide as the container, but never more than 65ch" */
.article { width: min(100%, 65ch); }
/* "at least 44px tall — bigger if the content needs it" */
.button { height: max(44px, 2.5em); }
/* "16px of padding, or 4% of the width, whichever is bigger" */
.section { padding-inline: max(16px, 4vw); }min() is a ceiling: it stops a value growing. max() is a floor: it stops a value shrinking. If you keep mixing them up, you are not alone: that confusion is exactly why clamp() exists.
clamp()
clamp(MIN, PREFERRED, MAX) is max(MIN, min(PREFERRED, MAX)) with the words in the order you would say them. The preferred value is usually something viewport-relative, so the result glides between the two limits as the screen changes.
css
/* Fluid type: 1rem on small screens, 1.25rem on large, smooth in between */
body { font-size: clamp(1rem, 0.9rem + 0.5vw, 1.25rem); }
/* A content column that is never cramped and never too wide */
main { width: clamp(320px, 90%, 1100px); }
/* Space between sections that breathes on big screens */
section + section { margin-top: clamp(2rem, 8vh, 6rem); }Why the preferred value has a rem part
clamp(1rem, 2vw, 2rem) works, but pure vw ignores the user's font-size setting: if someone has set their browser to a larger default text size, 2vw is the same size for them as for everyone else. Adding a rem term, 0.9rem + 0.5vw, keeps the size tied to their preference. Browser zoom is unaffected either way.
A realistic example: a sidebar layout without a breakpoint
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sidebar with math functions</title>
<style>
.layout { display: flex; flex-wrap: wrap; gap: clamp(12px, 2vw, 32px); }
aside { flex: 1 1 clamp(160px, 20%, 260px); background: #e0f2fe; padding: 16px; }
main { flex: 999 1 min(100%, 40ch); padding: 16px; }
body { font-family: system-ui, sans-serif; margin: 0; }
</style>
</head>
<body>
<div class="layout">
<aside>Sidebar</aside>
<main>
<h2>Main content</h2>
<p>Narrow the window: the sidebar shrinks to its minimum first, then the main column wraps below it. No media query is involved.</p>
</main>
</div>
</body>
</html>The sidebar's ideal width is 20% of the row, held between 160px and 260px. The main column asks for 40ch and grows to take the rest; when 40ch no longer fits beside the sidebar, flex-wrap drops it underneath. All the "at what width should this stack?" reasoning is in the values themselves.
The newer functions
Since 2023–2024 all major browsers also support round(), mod(), rem(), abs(), sign(), pow(), sqrt(), hypot(), and the trigonometric functions sin(), cos(), tan(), asin(), acos(), atan(), atan2(). They are handy for things like snapping a fluid size to a pixel grid, round(1.375rem, 1px), or placing items on a circle. Check support before relying on them in anything that must work on older phones.
Common mistakes
1. No spaces around + and −
calc(100%-20px) is invalid and the whole declaration is thrown away: the browser reads -20px as one negative token and there is no operator between it and 100%. It must be calc(100% - 20px). Multiplication and division do not need the spaces, but write them anyway for consistency.
2. A unitless zero
calc(0 + 10px) is invalid. Inside calc(), 0 is a number, not a length, and you cannot add a number to a length. Write 0px. This catches people who are used to writing margin: 0 everywhere else.
3. clamp() with the limits the wrong way round
clamp(3rem, 5vw, 1.5rem), minimum larger than maximum, is not an error, and the browser resolves it to the minimum every time. Your heading is stuck at 3rem and nothing in DevTools says why. If a clamp is not moving, check the order first.
4. Dividing by a value with a unit
calc(100% / 3px) is invalid. Division needs a plain number on the right; multiplication needs a plain number on one side. If you want a ratio of two lengths, you are usually looking for a percentage or aspect-ratio.
5. Expecting calc() to work in every property
It works wherever a numeric value is allowed: lengths, percentages, angles, times, numbers. It does not work inside a media query condition in older browsers, and it cannot produce a keyword. calc() in z-index or line-height is fine because those take numbers.
Browser support
calc() has worked everywhere since 2013, including old Edge and IE11. min(), max() and clamp() since Chrome 79, Firefox 75 and Safari 13.1 (2020). Nesting calc() inside the others has been fine since those same versions.
Related chapters
- CSS values and units: what rem, vw, ch and % actually measure
- CSS functions reference
- Intrinsic sizing:
min-content,fit-contentand friends - Media queries, for the cases math functions cannot cover
CSS Math Functions exercises
Add two lengths
Make the side 100px plus 50px wide, worked out with calc().
The HTML your code works on
<style>
.wrap { width: 600px; }
</style>
<div class="wrap">
<div class="side">Side</div>
<div class="main">Main</div>
</div>Result
