Reading history

CSS calc(), min(), max() and clamp() Explained

6 min read 3 views

Use CSS maths functions: calc() with mixed units and variables, min() for capped widths, max() for minimums and clamp() for fluid typography.

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 % and px with calc()
  • 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 --sidebar once 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 uses clamp(16px, 6cqi, 30px): it grows with the box, from 16px in the narrow box to 30px in the wide one.
  • cqi is a container query unit: 1cqi is 1% of the width of the nearest element with container-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 use vw, 1% of the window width, as in clamp(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 replaces width: 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, and max() sets a minimum.

CSS Maths Functions

FunctionReturnsTypical use
calc(100% - 60px)The result of the sumSpace left after a fixed element
min(90%, 1100px)The smallest valuePage container that stops growing
max(16px, 1vw)The largest valueSizes that must not get too small
clamp(1rem, 3vw, 2rem)Preferred value within limitsFluid headings and spacing

Common Mistakes

  1. Missing spaces around - and + inside calc().
  2. Putting the values of clamp() in the wrong order: it is always minimum, preferred, maximum.
  3. Using only vw for font sizes. Text sized purely in vw does not grow when users zoom; mix in rem, as in clamp(1rem, 0.8rem + 1vw, 1.5rem).
  4. Mixing up min() and max(). Read min(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.

Next: Project: Build a Product Landing Page.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *