On this page10 sections
  1. Learning Objectives
  2. 1. calc()
  3. 2. min(): the Smaller Value
  4. 3. max(): the Larger Value
  5. 4. clamp(min, preferred, max)
  6. 5. Newer Functions
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 029: Math Functions: calc, min, max & clamp

Category6: CSS in Depth
Lecture029 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelIntermediate

CSS can do arithmetic. calc() mixes units, as in "100% minus 80 pixels". min(), max() and clamp() pick values that adapt to the screen, often replacing whole media queries with a single line.


Learning Objectives

  • Combine units with calc()
  • Choose the smaller or larger of several values with min() and max()
  • Keep a value within limits with clamp()
  • Recognise newer functions such as round()

1. calc()

Example

html
<style>
  .sidebar { float: left; width: 120px; background: #fff3bf; padding: 8px; box-sizing: border-box; height: 80px; }
  .main    { float: left; width: calc(100% - 120px); background: #d0ebff; padding: 8px; box-sizing: border-box; height: 80px; }
</style>

<div class="sidebar">120px</div>
<div class="main">calc(100% - 120px): fills exactly the rest</div>
  • You can mix %, px, rem, vw and more.
  • Put spaces around + and -: calc(100% - 120px), not calc(100%-120px).

More uses:

css
.hero   { height: calc(100vh - 64px); }      /* full screen minus a 64px header */
.col    { width: calc(100% / 3); }           /* one third */
.space  { margin-top: calc(1rem * 2); }

2. min(): the Smaller Value

Example: drag the preview corner:

html
<div style="width: min(100%, 400px); background: #e6fcf5; padding: 10px; box-sizing: border-box;">
  width: min(100%, 400px). Never wider than 400px, and shrinks on small screens.
</div>

min() is a good way to set a maximum.


3. max(): the Larger Value

Example

html
<p style="font-size: max(16px, 2vw);">max(16px, 2vw): grows on big screens, but never below 16px.</p>

max() is a good way to set a minimum.


4. clamp(min, preferred, max)

Example: drag the corner and watch the heading:

html
<h1 style="font-size: clamp(1.5rem, 5vw, 3rem);">Fluid heading</h1>
<p style="padding: clamp(8px, 3vw, 32px); background: #f3f0ff;">Fluid padding too.</p>

The value follows the preferred one (5vw), but never goes below the minimum or above the maximum.

FunctionUse it for
calc(a - b)Mixing units
min(a, b)"At most…" (an upper limit)
max(a, b)"At least…" (a lower limit)
clamp(lo, ideal, hi)"Ideally this, but between these limits"

5. Newer Functions

css
width: round(33.33%, 10px);   /* round to the nearest 10px */
rotate: calc(sin(45deg) * 1turn);

Browsers also support round(), mod(), rem(), abs(), sign(), sin(), cos() and tan(), which are handy for creative layouts and animations.


Common Beginner Mistakes

1. No spaces around + and -. calc(100%-20px) is invalid.

2. Mixing up min and max. min() gives an upper limit; max() gives a lower limit.

3. vw text with no limits. It becomes tiny on phones. Wrap it in clamp().


Practice Questions

  1. Write a width that's the full width minus 2rem.
  2. What does width: min(90%, 800px) do?
  3. Write a font size that's 4vw, but at least 1rem and at most 2.5rem.
  4. Which function gives a lower limit?
  5. What's wrong with calc(50%+10px)?

Practice Answers

  1. width: calc(100% - 2rem);
  2. The element is 90% wide, but never more than 800px.
  3. font-size: clamp(1rem, 4vw, 2.5rem);
  4. max()
  5. There must be spaces around +: calc(50% + 10px).

Key Takeaways

  • calc() mixes units; keep spaces around + and -.
  • min() caps a value; max() sets a floor.
  • clamp(min, ideal, max) gives fluid sizes with limits, often replacing media queries.