Learn CSS Functions

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…

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>

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%); }

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.

The result at three screen widths:

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?

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.

What you will learn