Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 3.7 — CSS Maths Functions
CSS · Example 1 of 3

calc, min, max and clamp in a 400px box

100% is 400px here. Work out each value before reading the console.

calc() does arithmetic with mixed units, min() and max() pick the smaller or larger of several values, and clamp(MIN, PREFERRED, MAX) keeps a fluid value between limits; together they make sizes that adapt smoothly without media queries.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Make an image wrapper .media that is 50% of its parent, but never narrower than 200px and never wider than 480px.

02

Lay out three .col boxes side by side with display: inline-block, each exactly one third of the parent minus a 1rem gap share, using calc().

03

Write a fluid h2 that is 18px at a 360px screen and 28px at a 1360px screen.

Explain it without notes

01

Why does calc() need spaces around + and - but not around * and /?

02

Explain min() and max() and why they feel backwards.

03

Write the formula clamp() is equivalent to, and explain each argument.

04

How do you build an accessible fluid font size between two screen widths?

calc, min, max and clamp in a 400px box

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

calc(100% - 2 * 20px) = 360px
min(100%, 300px) = 300px
max(50%, 250px) = 250px
clamp(100px, 50%, 180px) = 180px