Sass math.clamp()

Beginner
9 min read
Updated: Nov 2026
5 examples
Dart Sass 1.25+

Definition and Usage

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+.

Official samples.

math.clamp(-1, 0, 1) → 0 · math.clamp(1px, -1px, 10px) → 1px · math.clamp(-1in, 1cm, 10mm) → 10mm.

Compile-time clamp.

Sass writes a fixed number into CSS. For fluid viewport math that must stay dynamic, use CSS clamp() instead.

Argument order is min, number, max.

Same shape as CSS clamp(), but resolved at compile time. Mixing incompatible units (for example px with em) fails—check with math.compatible.

Syntax

SCSS
@use "sass:math";

math.clamp($min, $number, $max); //=> number

math.clamp(-1, 0, 1);              // 0
math.clamp(1px, -1px, 10px);       // 1px
math.clamp(-1in, 1cm, 10mm);       // 10mm

// Compatible absolute lengths convert; relative mixes fail
// math.clamp(1px, 1em, 2px); // error

// Module API — prefer math.clamp after @use "sass:math"

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.

Bounds and Units

How the middle argument is forced into range:

SituationResult
$number < $min$min
$min ≤ $number ≤ $max$number
$number > $max$max
All unitlessOK
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.

SCSS Examples

Examples target Dart Sass 1.25+ with @use "sass:math". Open View Compiled CSS for verified output (matches official Sass docs samples).

Example 1 — Official Docs Sample (Unitless)

Zero sits between -1 and 1.

SCSS
@use "sass:math";

@debug math.clamp(-1, 0, 1); // 0

.basic {
  --a: #{math.clamp(-1, 0, 1)};
}

How It Works

  1. Min is -1, max is 1.
  2. Middle value 0 is inside the range.
  3. Result is 0 unchanged.
  4. Matches the official Sass docs sample.

Example 2 — Official Docs Sample (Same Units)

A preferred value below the minimum is lifted to the floor.

SCSS
@use "sass:math";

@debug math.clamp(1px, -1px, 10px); // 1px

.units {
  --b: #{math.clamp(1px, -1px, 10px)};
}

How It Works

  1. Range is 1px…10px.
  2. Preferred -1px is below the floor.
  3. Clamp returns $min → 1px.
  4. Units stay on the result.

Example 3 — Compatible Absolute Lengths

Official sample: absolute length units convert before clamping.

SCSS
@use "sass:math";

@debug math.clamp(-1in, 1cm, 10mm); // 10mm

.convert {
  --c: #{math.clamp(-1in, 1cm, 10mm)};
}

How It Works

  1. in, cm, mm are compatible absolutes.
  2. 1cm equals 10mm.
  3. Max is 10mm, so the result lands on the upper bound.
  4. Matches the official Sass docs sample.

Example 4 — Below, Inside, and Above

Three preferred widths against the same 10px…20px band.

SCSS
@use "sass:math";

.bounds {
  --low: #{math.clamp(10px, 5px, 20px)};
  --mid: #{math.clamp(10px, 15px, 20px)};
  --high: #{math.clamp(10px, 25px, 20px)};
}

How It Works

  1. 5px is below → 10px.
  2. 15px is inside → stays 15px.
  3. 25px is above → 20px.
  4. Same API for any compatible numeric tokens.

Example 5 — Design Tokens into CSS

Clamp width and font-size tokens before they reach the stylesheet.

SCSS
@use "sass:math";

$min: 12px;
$preferred: 18px;
$max: 24px;

.box {
  width: math.clamp($min, $preferred, $max);
  font-size: math.clamp(0.875rem, 1.125rem, 1.25rem);
}

How It Works

  1. Both preferred values sit inside their ranges.
  2. Sass emits fixed lengths — no CSS clamp() left.
  3. Useful when tokens must be validated at build time.
  4. Keep CSS clamp() when the middle term needs vw/vh at runtime.

Edge Cases & Pitfalls

Most clamp bugs are unit mismatches or expecting a live CSS function in the output.

units

Compatible units required

px with em fails. Use math.compatible before clamping mixed tokens.

order

Min, number, max

Swapping args changes which value is treated as the preferred middle.

stage

Compile time, not runtime

Sass cannot clamp a live CSS custom property or viewport unit that changes in the browser.

css

Sass vs CSS clamp()

Need fluid typography with vw? Leave CSS clamp() in the stylesheet instead of resolving it in Sass.

Sass Support

Prefer the module form with Dart Sass 1.25+.

APIStatus
math.clamp()Dart Sass 1.25+ via @use "sass:math"
InputThree numbers: unitless or compatible units
Global clamp()Not the documented Sass API — use math.clamp
LibSass / Ruby SassNo modern sass:math clamp

Compile with the Dart Sass CLI.

Key Takeaways

  • Clamps $number between $min and $max.
  • Units must match (or all be unitless).
  • 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).

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.

Next: Sass math.compatible()

Test whether two numbers have units you can safely add or compare.

math.compatible() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful