Lesson 25 of 50
cssCSS Functions – calc(), clamp(), min(), max()
8 mins
Course progress50%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 25 of 50
css8 mins
Keep these ideas in mind as you work through the lesson.
Functions give CSS programming‑like capabilities, letting you calculate values on the fly.
calc() – ArithmeticCombine different units and operators.
.content { width: calc(100% - 2rem); }
.sidebar { height: calc(100vh - 4rem); }
Use spaces around
+,-,*,/for readability – browsers ignore them.
clamp() – Fluid values with limitsfont-size: clamp(1rem, 2.5vw, 2rem);
The browser picks the preferred middle value (2.5vw) but never goes below 1rem or above 2rem.
min() & max() – Pick extremes.container { max-width:max(20rem, 50%); }
50% of the viewport is larger than 20rem, the max() returns 50%; otherwise 20rem.clampsection {
padding: clamp(1rem, 2vw, 3rem);
}
On a small phone the padding stays at 1 rem, on a large desktop it grows to 3 rem, and in‑between it scales fluidly with the viewport width.
.card {
width: min(calc(100% - 2rem), 400px);
font-size: clamp(0.9rem, 1.5vw, 1.2rem);
}
min()ensures the card never exceeds 400 px, while its text stays fluid.
All four functions are widely supported in Chrome, Edge, Firefox, and Safari (including iOS). No vendor prefixes required.