A page has a sidebar that is 250px wide, and the content next to it has to fill the rest of the page's width. The problem: that rest keeps changing. It is one size on a laptop and another on a phone. No single px number fits every screen.
Luckily, CSS can do the maths itself. calc(), min(), max() and clamp() are functions that produce a size. The browser calculates that size again every time the available space changes.
Doing maths with calc()
This example has a sidebar and the content inside .layout, then the CSS:
<div class="layout">
<aside class="sidebar">Sidebar</aside>
<div class="content">Content</div>
</div>
<style>
.layout { display: flex; }
.sidebar { width: 250px; }
.content { width: calc(100% - 250px); }
</style>
calc( ... )is the function for maths. Inside its parentheses is one formula.100%is the full width of.content's parent,.layout.- 250pxsubtracts the sidebar's width from that. Units can be mixed: here, a percentage minus pixels.
The result: if .layout is 1000px wide, .content is 750px wide. If .layout narrows to 600px, .content narrows to 350px. The sidebar stays 250px, and the content fills the rest.
One thing people often forget: the + and - signs inside calc() need a space on each side. Without spaces, as in width: calc(100%-250px), the browser reads -250px as a negative number, not a subtraction. Then the whole value is invalid, and the browser ignores it. Write calc(100% - 250px).
Taking the smallest or the largest: min() and max()
These two rules use min() and max() for a width:
.card { width: min(400px, 100%); }
.menu { width: max(200px, 20%); }
min(400px, 100%)uses the smallest value. In a parent 1000px wide, 100% = 1000px, so it uses 400px. On a phone screen, with a parent 300px wide, 100% = 300px. That is smaller than 400px, so the width is 300px.max(200px, 20%)uses the largest value. In a parent 600px wide, 20% = 120px, so it uses 200px.
The names seem the opposite of what they do: min() sets an upper limit (the card is 400px wide at most), and max() sets a lower limit (the menu is 200px wide at least).
Holding a value between two limits: clamp()
This rule sets a heading's font size with clamp():
.title { font-size: clamp(1rem, 4vw, 3rem); }
clamp() takes three values: the lower limit, the ideal value, the upper limit.
1remis the lower limit: the smallest size.remfollows the page's base font size, usually 16px.4vwis the ideal value.1vwis 1% of the viewport (screen) width, so4vwis 4% of the screen width.3remis the upper limit: the largest size, usually 48px.
The result at three screen widths:
- 1000px screen: 4vw = 40px. That is between the two limits, so the text is 40px.
- 300px screen: 4vw = 12px. Too small, so it uses the lower limit, 16px.
- 1600px screen: 4vw = 64px. Too big, so it uses the upper limit, 48px.
The heading grows with the screen width without a media query, but it never gets too small or too big.
An ideal value made only of vw, like 4vw, doesn't change when the user makes the text bigger in the browser settings: on a 1000px screen, the heading stays 40px. The better form mixes rem and vw, for example clamp(1rem, 0.75rem + 2vw, 3rem). On the same 1000px screen, that gives 32px with the default text size of 16px, and 38px when the user raises it to 24px.
⚠️ Why does the formula do nothing?
- A missing comma.
min(400px 100%)is not valid. The values insidemin(),max()andclamp()must be separated by commas. - A number with no unit.
calc(100% - 20)is not valid, because 20 with no unit cannot be subtracted from a width. Write20px. clamp()values in the wrong order. The first value is always the lower limit, and the last value is always the upper limit. If you swap them, as inclamp(3rem, 4vw, 1rem), the lower limit (3rem) always wins. So the size never changes.
TL;DR: calc(100% - 250px) makes the browser calculate a size from a formula. Units can be mixed. The + and - signs must have a space on each side. min() uses the smallest value inside its parentheses, so it works as an upper limit: min(400px, 100%) is never more than 400px. max() uses the largest value, so it works as a lower limit. clamp(1rem, 4vw, 3rem) takes a lower limit, an ideal value and an upper limit: the ideal value is used while it stays between the two limits.
⚡ Doing maths with calc
Write:
.layout { display: flex; }
.sidebar { width: 250px; }
.content { width: calc(100% - 250px); }
Guess first: does calc(100%-250px), with no spaces around the minus sign, work?
No. The browser reads -250px as a negative number, not a subtraction, so it ignores the whole value.
Meaning: calc() is the function for maths. Here the width of .content is its parent's width (100%) minus the sidebar's width, 250px. The browser calculates the result again every time the parent's width changes. Units can be mixed. The + and - signs must have a space on each side.
⚡ The smallest or the largest
Write:
.card { width: min(400px, 100%); }
Guess first: min(400px, 100%) uses the smallest of the two values. So is 400px here the largest width the card can have (an upper limit) or the smallest (a lower limit)?
An upper limit. When 100% is more than 400px, the card uses 400px. So the card is 400px wide at most.
Meaning: min() sets an upper limit. max() is the opposite: it uses the largest value, so it sets a lower limit. The values inside the parentheses are separated by commas.
⚡ Held between two limits
Write:
.title { font-size: clamp(1rem, 4vw, 3rem); }
Meaning: clamp() takes three values: the lower limit (1rem, usually 16px), the ideal value (4vw, 4% of the screen width), then the upper limit (3rem, usually 48px). The heading's font size grows with the screen width without a media query, but it never goes past the two limits.