Sass math.clamp() Function

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:math · Dart Sass 1.25+

What You’ll Learn

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

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.

📝 Syntax

Load the math module, then call the function:

styles.scss
@use "sass:math";

math.clamp($min, $number, $max)

Parameters

ParameterTypeRequiredDescription
$minNumberYesLower bound. Returned when $number is less than this value.
$numberNumberYesThe value to clamp. Returned unchanged when it sits between $min and $max.
$maxNumberYesUpper bound. Returned when $number is greater than this value.

All three arguments must have compatible units, or all be unitless.

Return value

TypeSituationResult
Number$number < $min$min
Number$min$number$max$number
Number$number > $max$max

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

📏 How Units Work

Compatible units can be mixed (Sass converts as needed). Incompatible units fail at compile time.

CallResultNotes
math.clamp(-1, 0, 1)0All unitless
math.clamp(1px, -1px, 10px)1pxSame unit family
math.clamp(-1in, 1cm, 10mm)10mmCompatible absolute lengths
math.clamp(8px, 20px, 32px)20pxPreferred value inside range
math.clamp(10px, 2em, 40px)Error: incompatible units

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS receives a plain number—no clamp() call remains unless you wrote CSS clamp yourself.

Implementationmath.clamp()Notes
Dart Sass 1.25+YesPreferred; needs @use "sass:math"
Dart Sass < 1.25NoUpgrade Dart Sass to use clamp
LibSass / Ruby SassNo module APIUse Dart Sass, or emulate with min/max logic

⚡ Quick Reference

GoalCode
Import math@use "sass:math";
Clamp a valuemath.clamp($min, $n, $max)
Raise to minimummath.clamp(8px, 2px, 24px)8px
Cap at maximummath.clamp(8px, 40px, 24px)24px
Keep preferredmath.clamp(8px, 16px, 24px)16px
Debug while learning@debug math.clamp(1px, -1px, 10px);

📋 clamp vs min / max

You can build a clamp with nested min/max, but math.clamp is clearer.

GoalWith math.clampWith min / max
Bound $nmath.clamp($min, $n, $max)math.min($max, math.max($min, $n))
ReadabilityOne clear callEasy to reverse min/max by mistake
Intent“Keep inside a range”Two separate bounds composed

📋 Sass math.clamp() vs CSS clamp()

Sass math.clamp()CSS clamp()
When it runsCompile time (Dart Sass)In the browser
Appears in CSS output?No — only the resultYes — as clamp(...)
Fluid / viewport math?No (values must be known)Yes (e.g. clamp(1rem, 2vw, 2rem))
Best forFixed tokens & SCSS formulasResponsive fluid sizing

If the preferred value depends on the viewport, use CSS clamp(). If every input is already a known Sass number, use math.clamp() and ship a simple length.

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.

styles.scss
@use "sass:math";

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

.meter {
  opacity: math.clamp(0, 0.65, 1);
}

How It Works

0.65 sits between 0 and 1, so the compiled CSS keeps 0.65.

Example 2 — Compatible Length Units

Absolute lengths can mix when Sass can convert them.

styles.scss
@use "sass:math";

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

.box {
  // preferred -1px is below min 1px → 1px
  border-width: math.clamp(1px, -1px, 10px);
}

How It Works

The preferred -1px is below the floor, so Sass emits the minimum 1px.

📈 Practical Patterns

Raise floors, cap ceilings, and protect design tokens.

Example 3 — Raise a Value to a Floor

Useful when a calculation might go too small.

styles.scss
@use "sass:math";

$computed: 4px;
$min: 12px;
$max: 48px;

.icon {
  width: math.clamp($min, $computed, $max);
  height: math.clamp($min, $computed, $max);
}

How It Works

4px is below 12px, so both dimensions become the minimum.

Example 4 — Cap a Value at a Ceiling

Stop spacing or type from growing past a design limit.

styles.scss
@use "sass:math";

$gap: 40px;

.section {
  padding-block: math.clamp(8px, $gap, 24px);
}

How It Works

40px exceeds 24px, so the compiled padding is capped at the maximum.

Example 5 — Protect a Theme Token

Clamp a shared radius token before it reaches components.

styles.scss
@use "sass:math";

$radius-raw: 18px;
$radius: math.clamp(4px, $radius-raw, 16px);

.card {
  border-radius: $radius;
}

.chip {
  border-radius: math.clamp(4px, $radius-raw * 0.25, 16px);
}

How It Works

The card hits the 16px ceiling. The chip’s smaller product 4.5px stays inside the range unchanged.

🚀 Real-World Use Cases

  • Design tokens — keep radius, gap, or font-size numbers inside brand limits.
  • 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.

⚠️ 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.

💡 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

Key Takeaways

Knowledge Unlocked

Five things to remember about math.clamp()

Compile-time min / preferred / max—compatible units required.

5
Core concepts
📦 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.

Conclusion

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.

Continue with math.compatible(), math.ceil(), or math.abs().

More Sass Math

Continue with unit compatibility checks in the math module.

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.

5 people found this page helpful