math.clamp($min, $number, $max) restricts $number to the range between $min and $max. Below the floor it returns $min; above the ceiling it returns $max; otherwise it returns $number. All three must have compatible units or all be unitless. Load it with @use "sass:math" on Dart Sass 1.25+.
Pattern: choose bounds → pass the preferred value in the middle → emit a safe length or count. Pair with math.min / math.max when you only need one side of the range.
Reference
Bounds and Units
How the middle argument is forced into range:
Situation
Result
$number < $min
$min
$min ≤ $number ≤ $max
$number
$number > $max
$max
All unitless
OK
Compatible units (e.g. cm/mm)
OK — result uses a converted unit
Incompatible units (e.g. px/em)
Compile error
Why 10mm in the docs?
1cm equals 10mm, so with max 10mm the preferred value sits on the upper bound and Sass emits 10mm.
Resolves at compile time — CSS gets a fixed number.
Dart Sass 1.25+ via @use "sass:math".
One line: when a Sass number must stay between two bounds at build time, call math.clamp($min, $number, $max).
Practice
Compile Notes
Practical checklist:
checklist.txt
1. @use "sass:math"
2. Pass min, preferred number, max (compatible units)
3. Expect a fixed value inside the range
4. Use CSS clamp() for viewport/fluid values
5. Check math.compatible when units differ
Frequently Asked Questions
math.clamp($min, $number, $max) returns $number when it sits between $min and $max. If $number is below $min it returns $min; if it is above $max it returns $max. All three values need compatible units (or all unitless).
Add @use "sass:math"; then call math.clamp($min, $number, $max). Example: math.clamp(1px, -1px, 10px) compiles to 1px.
Unlike abs() or ceil(), Sass documents clamp mainly as math.clamp() on the sass:math module. Prefer the module form. CSS has its own clamp() that runs in the browser.
The three arguments must have compatible units (for example px with px, or cm with mm) or all be unitless. Incompatible mixes (like px with em) cause a compile error.
Same idea, different stage. Sass math.clamp() runs when you compile SCSS and writes a fixed number. CSS clamp() stays in the stylesheet and resolves in the browser—useful for fluid typography and viewport math.
Built-in modules need Dart Sass. math.clamp() itself is available since Dart Sass 1.25.0. LibSass and Ruby Sass do not provide sass:math the same way.