math.clamp() is a bounding helper in the sass:math module. It forces a value to stay between a minimum and a maximum. You will learn the three-argument order, unit rules, Dart Sass requirements, how it differs from CSS clamp(), and five compiled examples.
01
Concept
Min · value · max
02
Module
@use "sass:math"
03
Units
Must be compatible
04
Since
Dart Sass 1.25+
05
Vs CSS
clamp()
06
Practice
5 examples
Concept
What Is math.clamp()?
Clamping means “keep this number inside a safe range.” If the value is too low, use the minimum. If it is too high, use the maximum. Otherwise keep the value as-is.
math.clamp(-1, 0, 1) → 0 (inside the range)
math.clamp(1px, -1px, 10px) → 1px (raised to the min)
math.clamp(-1in, 1cm, 10mm) → 10mm (capped at the max)
Use it for design tokens, opacity-like scales, spacing that must stay between two bounds, or any compile-time number that should never leave a corridor.
💡
Beginner tip
Argument order is $min, then $number, then $max—the same mental model as CSS clamp(min, preferred, max), but Sass resolves it while compiling.
Foundation
📝 Syntax
Load the math module, then call the function:
styles.scss
@use "sass:math";
math.clamp($min, $number, $max)
Parameters
Parameter
Type
Required
Description
$min
Number
Yes
Lower bound. Returned when $number is less than this value.
$number
Number
Yes
The value to clamp. Returned unchanged when it sits between $min and $max.
$max
Number
Yes
Upper bound. Returned when $number is greater than this value.
All three arguments must have compatible units, or all be unitless.
Return value
Type
Situation
Result
Number
$number < $min
$min
Number
$min ≤ $number ≤ $max
$number
Number
$number > $max
$max
Modules
📦 Loading sass:math
math.clamp() lives only on the module API in modern Sass docs—load sass:math with @use.
styles.scss
// Recommended — namespaced
@use "sass:math";
$size: math.clamp(12px, 18px, 24px);
// Optional — bring members into the current namespace
@use "sass:math" as *;
$size: clamp(12px, 18px, 24px); // math module member
// Optional — custom namespace
@use "sass:math" as m;
$size: m.clamp(12px, 18px, 24px);
⚠️
Put @use first
Keep @use "sass:math"; near the top of the file. Do not confuse Sass math.clamp with CSS clamp() written into plain CSS output.
Numbers
📏 How Units Work
Compatible units can be mixed (Sass converts as needed). Incompatible units fail at compile time.
Call
Result
Notes
math.clamp(-1, 0, 1)
0
All unitless
math.clamp(1px, -1px, 10px)
1px
Same unit family
math.clamp(-1in, 1cm, 10mm)
10mm
Compatible absolute lengths
math.clamp(8px, 20px, 32px)
20px
Preferred value inside range
math.clamp(10px, 2em, 40px)
—
Error: incompatible units
Support
🛠 Compatibility
This is about Sass compilers, not browsers. Compiled CSS receives a plain number—no clamp() call remains unless you wrote CSS clamp yourself.
Implementation
math.clamp()
Notes
Dart Sass 1.25+
Yes
Preferred; needs @use "sass:math"
Dart Sass < 1.25
No
Upgrade Dart Sass to use clamp
LibSass / Ruby Sass
No module API
Use Dart Sass, or emulate with min/max logic
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import math
@use "sass:math";
Clamp a value
math.clamp($min, $n, $max)
Raise to minimum
math.clamp(8px, 2px, 24px) → 8px
Cap at maximum
math.clamp(8px, 40px, 24px) → 24px
Keep preferred
math.clamp(8px, 16px, 24px) → 16px
Debug while learning
@debug math.clamp(1px, -1px, 10px);
Compare
📋 clamp vs min / max
You can build a clamp with nested min/max, but math.clamp is clearer.
Goal
With math.clamp
With min / max
Bound $n
math.clamp($min, $n, $max)
math.min($max, math.max($min, $n))
Readability
One clear call
Easy to reverse min/max by mistake
Intent
“Keep inside a range”
Two separate bounds composed
Compare
📋 Sass math.clamp() vs CSS clamp()
Sass math.clamp()
CSS clamp()
When it runs
Compile time (Dart Sass)
In the browser
Appears in CSS output?
No — only the result
Yes — as clamp(...)
Fluid / viewport math?
No (values must be known)
Yes (e.g. clamp(1rem, 2vw, 2rem))
Best for
Fixed tokens & SCSS formulas
Responsive fluid sizing
If the preferred value depends on the viewport, use CSSclamp(). If every input is already a known Sass number, use math.clamp() and ship a simple length.
Hands-On
Examples Gallery
Each example uses @use "sass:math". Open View Compiled CSS to see what Dart Sass emits.
📚 Getting Started
Official-style clamps with unitless and length values.
Example 1 — Basic math.clamp()
When the preferred value is already inside the range, it passes through.
Opacity / scales — clamp unitless ratios between 0 and 1 (or 0 and 100).
Icon and avatar sizes — never smaller than a floor or larger than a ceiling.
Generated maps — after loops or maps, clamp each value before emit.
Safer theming — accept user-provided numbers but pin them to a corridor.
Fixed vs fluid — use Sass clamp for known numbers; CSS clamp for viewport fluid type.
🧠 How Compilation Works
1
Write SCSS
Call math.clamp($min, $number, $max) after @use.
Source
2
Check units
Sass verifies the three numbers are compatible (or unitless).
Validate
3
Pick min, value, or max
Too low → min. Too high → max. Otherwise keep the number.
Clamp
4
✓
Plain CSS ships
The browser only receives the final bounded number.
Watch Out
⚠️ Common Pitfalls
Wrong argument order — it is min, preferred, max—not preferred first.
Incompatible units — mixing px with em fails; keep a shared unit family.
Min greater than max — keep $min ≤ $max or results become confusing.
Expecting fluid CSS — Sass clamp cannot use vw the way CSS clamp can.
Old Dart Sass — need 1.25+ for math.clamp; LibSass lacks the module.
Pro Tips
💡 Best Practices
✅ Do
Use @use "sass:math" and math.clamp() in new SCSS
Keep all three arguments in a compatible unit family
Use Sass clamp for known tokens; CSS clamp for fluid type
Name bounds clearly ($radius-min, $radius-max)
Prefer Dart Sass 1.25+
❌ Don’t
Expect the browser to re-evaluate math.clamp
Mix relative and absolute units carelessly
Replace CSS fluid clamp() with Sass when viewport math is required
Swap min and max by accident
Rely on LibSass for sass:math
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about math.clamp()
Compile-time min / preferred / max—compatible units required.
5
Core concepts
📝01
Call
clamp(min, n, max)
API
📦02
Module
sass:math
@use
📏03
Units
compatible
Rule
⚡04
When
compile time
Sass
✓05
Since
Dart Sass 1.25+
Tooling
❓ 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.
Did you know?
Official Sass docs list math.clamp under Bounding Functions with ceil, floor, round, min, and max. Clamp is the one that speaks in ranges instead of single-direction rounding.
math.clamp() keeps a number between two bounds at compile time. Pass compatible units (or none), prefer Dart Sass 1.25+, and choose CSS clamp() instead when the preferred value must stay fluid with the viewport.