Plain CSS values are fixed: 300px or 50%. Real layouts often need a mix, such as “the full width minus the sidebar” or “a heading that grows with the screen, within limits”. CSS has four maths functions for this. calc() does arithmetic with different units, min() and max() pick the smaller or larger of several values, and clamp() keeps a value between a minimum and a maximum. Together they replace many media queries.
In this lesson you will learn
- Mixing units such as
%andpxwithcalc() - Combining
calc()with CSS variables - Using
min()for “fluid but capped” widths - Using
max()for minimum sizes - Fluid typography with
clamp()
You should know CSS units and CSS variables.
calc(): Arithmetic with Mixed Units
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>calc()</title>
<style>
:root { --sidebar: 140px; --gap: 12px; }
body { font-family: sans-serif; margin: 12px; font-size: 14px; }
.layout { display: flex; gap: var(--gap); }
.side { width: var(--sidebar); background: #ffe0b2; padding: 8px; box-sizing: border-box; }
.main { /* mix % and px in one value */
width: calc(100% - var(--sidebar) - var(--gap));
background: #e1f5fe; padding: 8px; box-sizing: border-box;
}
.cols { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.cols div { /* three equal columns including the gaps */
width: calc((100% - 2 * 10px) / 3);
background: #c5e1a5; padding: 6px; box-sizing: border-box;
}
.spaced { margin-top: calc(var(--gap) * 2); border-left: 4px solid #8e24aa; padding-left: 8px; }
</style>
</head>
<body>
<div class="layout">
<aside class="side">Sidebar 140px</aside>
<main class="main">Main: calc(100% - 140px - 12px)</main>
</div>
<div class="cols"><div>One third</div><div>One third</div><div>One third</div></div>
<p class="spaced">margin-top: calc(var(--gap) * 2) = 24px</p>
</body>
</html>
Result
calc(100% - 140px - 12px)mixes a percentage with pixels, something no single unit can express. The browser works it out at layout time, so it stays correct as the window changes size.- Variables work inside
calc():calc(100% - var(--sidebar) - var(--gap)). Change--sidebaronce and both the sidebar and the main area update. calc((100% - 2 * 10px) / 3)makes three equal columns that leave room for two 10px gaps.+and-must have a space on each side:calc(100% - 20px)works,calc(100%-20px)is invalid.*and/do not need spaces, but they make the expression easier to read.- In multiplication, at least one side must be a plain number, and in division the right side must be a number:
calc(var(--gap) * 2)is valid,calc(10px * 2px)is not.
min(), max() and clamp()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>min(), max() and clamp()</title>
<style>
body { font-family: sans-serif; margin: 12px; font-size: 13px; }
.box { container-type: inline-size; border: 1px solid #90a4ae; margin-bottom: 8px; padding: 4px 8px; }
.w1 { width: 200px; } .w2 { width: 380px; } .w3 { width: 600px; max-width: 100%; box-sizing: border-box; }
.box h2 {
margin: 0;
font-size: clamp(16px, 6cqi, 30px); /* grows with the box, but never below 16px or above 30px */
color: #1565c0;
}
.card {
width: min(100%, 320px); /* the smaller of the two: full width on narrow screens */
padding: max(8px, 2cqi); /* the larger of the two: never less than 8px */
background: #fff3e0; box-sizing: border-box;
}
</style>
</head>
<body>
<div class="box w1"><h2>Fluid heading</h2><div class="card">min(100%, 320px)</div></div>
<div class="box w2"><h2>Fluid heading</h2><div class="card">min(100%, 320px)</div></div>
<div class="box w3"><h2>Fluid heading</h2><div class="card">min(100%, 320px)</div></div>
</body>
</html>
Result
clamp(MIN, PREFERRED, MAX)uses the preferred value but never goes below MIN or above MAX. The heading usesclamp(16px, 6cqi, 30px): it grows with the box, from 16px in the narrow box to 30px in the wide one.cqiis a container query unit: 1cqi is 1% of the width of the nearest element withcontainer-type: inline-size. The demo uses it so three boxes of different widths can be compared on one page. On a real page you would usually usevw, 1% of the window width, as inclamp(1.5rem, 4vw, 2.5rem).min(100%, 320px)picks the smaller value. The card fills narrow boxes but stops growing at 320px. This one line replaceswidth: 100%; max-width: 320px.max(8px, 2cqi)picks the larger value, so the padding grows with the box but never drops below 8px.- A useful memory aid:
min()sets a maximum, andmax()sets a minimum.
CSS Maths Functions
| Function | Returns | Typical use |
|---|---|---|
calc(100% - 60px) | The result of the sum | Space left after a fixed element |
min(90%, 1100px) | The smallest value | Page container that stops growing |
max(16px, 1vw) | The largest value | Sizes that must not get too small |
clamp(1rem, 3vw, 2rem) | Preferred value within limits | Fluid headings and spacing |
Common Mistakes
- Missing spaces around
-and+insidecalc(). - Putting the values of
clamp()in the wrong order: it is always minimum, preferred, maximum. - Using only
vwfor font sizes. Text sized purely invwdoes not grow when users zoom; mix inrem, as inclamp(1rem, 0.8rem + 1vw, 1.5rem). - Mixing up
min()andmax(). Readmin(100%, 320px)as “at most 320px”.
Practice Exercises
Exercise 1. Make a page container that is the window width minus 32px on phones, but at most 1000px wide, centered.
Show answer
.container {
width: min(100% - 32px, 1000px);
margin-inline: auto;
}
Inside min(), max() and clamp() you can write arithmetic directly without calc().
Exercise 2. Give h1 a size that is at least 28px, at most 48px, and 5% of the window width in between.
Show answer
h1 { font-size: clamp(28px, 5vw, 48px); }
Frequently Asked Questions
Can clamp() replace media queries?
For sizes, often yes: fluid font sizes, spacing and widths no longer need a breakpoint for each screen size. Media queries are still needed when the layout itself changes, such as moving a sidebar below the content.
Do these functions work in all browsers?
Yes. calc() has worked for many years, and min(), max() and clamp() work in every current browser. Container query units such as cqi are also supported in all current browsers.
Summary
calc()mixes units and works with variables; keep spaces around+and-.min()caps a value;max()sets a floor.clamp(min, preferred, max)creates fluid sizes within limits.- These functions reduce the need for media queries.
