TExcelTAD
Home › CSS › CSS Math Functions

CSS Math Functions

CSS can do arithmetic. Math functions let you compute values at render time — mixing units like % and px, clamping sizes between limits, or picking the smaller of two values. The browser recalculates them whenever the layout changes.

Overview

Function Returns Example
calc(expr) The result of an expression calc(100% - 60px)
min(a, b, …) The smallest value min(90%, 600px)
max(a, b, …) The largest value max(50vh, 300px)
clamp(min, ideal, max) ideal, limited between min and max clamp(1rem, 4vw, 2.5rem)
round(), mod(), rem(), abs(), sin()… Newer math functions in modern browsers round(up, 37px, 10px)

calc()

calc() supports +, -, * and /, and can mix different units — something no preprocessor can do, because % is only known at layout time.

Note: + and - must have spaces on both sides: calc(100% - 20px). calc(100%-20px) is invalid.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; }
  .sidebar { position: fixed; top: 0; left: 0; width: 90px; height: 100%; background: #37474f; color: white; padding: 8px; box-sizing: border-box; }
  .main { margin-left: 90px; width: calc(100% - 90px); padding: 10px; box-sizing: border-box; background: #eceff1; min-height: 100vh; }
  .third { display: inline-block; width: calc((100% - 20px) / 3); height: 50px; background: #26a69a; color: white; margin-right: 6px; text-align: center; line-height: 50px; }
  .third:last-child { margin-right: 0; }
  .main { font-size: 0; }
  .main > p { font-size: 15px; }
  .main > div { font-size: 15px; }
</style>
</head>
<body>
  <div class="sidebar">90px</div>
  <div class="main">
    <p>width: calc(100% - 90px)</p>
    <div class="third">⅓</div><div class="third">⅓</div><div class="third">⅓</div>
  </div>
</body>
</html>

min() and max()

min() picks the smaller value — perfect for "fluid, but no wider than…". max() picks the larger — "fluid, but no smaller than…".

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .card { width: min(90%, 360px); margin: 10px auto; padding: 12px; background: #e8eaf6; border-radius: 10px; }
  .tall { height: max(30vh, 70px); background: #ffccbc; display: grid; place-items: center; border-radius: 10px; }
</style>
</head>
<body>
  <div class="card">width: min(90%, 360px) — resize the frame: wide screens cap at 360px, narrow ones use 90%.</div>
  <div class="card tall">height: max(30vh, 70px)</div>
</body>
</html>

width: min(90%, 360px) replaces the older two-line pattern width: 90%; max-width: 360px;.

clamp() for fluid typography

clamp(MIN, PREFERRED, MAX) returns the preferred value but never goes below the minimum or above the maximum. It's the go-to tool for text that scales smoothly with the viewport.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 10px; }
  h1 { font-size: clamp(1.25rem, 7vw, 3rem); margin: 0 0 8px; color: #ad1457; line-height: 1.1; }
  p { font-size: clamp(14px, 2.5vw, 18px); }
  .pad { padding: clamp(8px, 4vw, 32px); background: #fce4ec; border-radius: 12px; }
</style>
</head>
<body>
  <div class="pad">
    <h1>Fluid headline</h1>
    <p>Drag the playground narrower or wider: the heading, text and padding all scale smoothly between their limits.</p>
  </div>
</body>
</html>

Tip: Combine a relative and a viewport unit in the preferred value, e.g. clamp(1rem, 0.8rem + 1.5vw, 2rem). Including rem keeps the text responsive to the user's zoom and font settings.

Combining with custom properties

Math functions work well with CSS variables, letting you derive many values from a few settings.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  :root { --space: 8px; --cols: 4; }
  body { font-family: system-ui, sans-serif; }
  .grid { display: flex; flex-wrap: wrap; gap: calc(var(--space) * 2); }
  .grid div {
    width: calc((100% - (var(--cols) - 1) * var(--space) * 2) / var(--cols));
    height: calc(var(--space) * 8);
    background: #5c6bc0; border-radius: var(--space);
  }
  .grid div:nth-child(even) { background: #9fa8da; }
</style>
</head>
<body>
  <p>Change <code>--cols</code> or <code>--space</code> and run again:</p>
  <div class="grid"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>
</body>
</html>

Test yourself

Which calc() expression is valid?

  1. calc(100%-20px)
  2. calc(100% - 20px)
  3. calc(100% minus 20px)
  4. calc[100% - 20px]

What does width: min(80%, 500px) produce in a 1000px-wide container?

  1. 800px
  2. 500px
  3. 80px
  4. 1000px

In clamp(16px, 4vw, 32px), what is the result when 4vw equals 40px?

  1. 16px
  2. 40px
  3. 32px
  4. 4vw