On this page10 sections
Lecture 029: Math Functions: calc, min, max & clamp
| Category | 6: CSS in Depth |
| Lecture | 029 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
CSS can do arithmetic.
calc()mixes units, as in "100% minus 80 pixels".min(),max()andclamp()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()andmax() - Keep a value within limits with
clamp() - Recognise newer functions such as
round()
1. calc()
Example
<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,vwand more. - Put spaces around
+and-:calc(100% - 120px), notcalc(100%-120px).
More uses:
.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:
<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
<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:
<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.
| Function | Use 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
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
- Write a width that's the full width minus 2rem.
- What does
width: min(90%, 800px)do? - Write a font size that's 4vw, but at least 1rem and at most 2.5rem.
- Which function gives a lower limit?
- What's wrong with
calc(50%+10px)?
Practice Answers
width: calc(100% - 2rem);- The element is 90% wide, but never more than 800px.
font-size: clamp(1rem, 4vw, 2.5rem);max()- 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.