Sass math.compatible() Function

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:math · Unit Functions

What You’ll Learn

math.compatible() is a unit helper in the sass:math module. It answers one question: can these two numbers work together? This page covers the boolean return, the global comparable() alias, how it protects add/compare/clamp calls, and five compiled examples.

01

Concept

Unit check

02

Module

@use "sass:math"

03

Returns

true / false

04

Global

comparable()

05

Guards

Add · compare

06

Practice

5 examples

What Is math.compatible()?

Sass numbers often carry CSS units (px, em, cm, and so on). Some pairs can be added or compared because Sass knows how to convert them. Others cannot—and mixing them throws a compile error.

math.compatible($number1, $number2) returns whether those units are compatible. Official docs put it under Unit Functions next to helpers that inspect units.

  • math.compatible(2px, 1px)true (same unit)
  • math.compatible(10cm, 3mm)true (convertible absolute lengths)
  • math.compatible(100px, 3em)false (absolute vs relative)
💡
Beginner tip

Think of it as a seatbelt check before math. If the answer is true, operations like $a + $b, math.min($a, $b), or math.clamp(...) are safe from unit errors. If it is false, fix the units first.

📝 Syntax

Load the math module, then call the function:

styles.scss
@use "sass:math";

math.compatible($number1, $number2)

Parameters

ParameterTypeRequiredDescription
$number1NumberYesFirst value to inspect (with or without units).
$number2NumberYesSecond value to compare against $number1’s units.

Return value

TypeMeaning
trueUnits are compatible (including unitless with lengths). Safe to add, subtract, or compare.
falseUnits are incompatible. Adding or comparing them will error.

📦 Loading sass:math

Prefer the module API. Official docs also document a global name, comparable(), for older stylesheets.

styles.scss
// Recommended — namespaced
@use "sass:math";
$ok: math.compatible(2px, 1px);

// Optional — bring members into the current namespace
@use "sass:math" as *;
$ok: compatible(2px, 1px);

// Optional — custom namespace
@use "sass:math" as m;
$ok: m.compatible(2px, 1px);

// Legacy global name (still works in many setups)
$ok: comparable(2px, 1px);
⚠️
Heads up from the docs

The global function is named comparable, but the sass:math member is compatible—chosen to describe what the function actually checks. New SCSS should use math.compatible().

📏 Which Units Are Compatible?

Compatible means Sass can place the values on the same measuring stick. Same unit always works. Convertible absolute lengths (like cm and mm) work. Absolute vs relative lengths (like px and em) do not.

CallResultNotes
math.compatible(2px, 1px)trueSame unit (official example)
math.compatible(10cm, 3mm)trueConvertible absolute lengths
math.compatible(100px, 3em)falseAbsolute vs relative
math.compatible(1, 2)trueBoth unitless
math.compatible(1, 1px)trueUnitless works with lengths
math.compatible(1deg, 1rad)trueCompatible angles
math.compatible(1s, 500ms)trueCompatible times
math.compatible(10%, 20px)falsePercent vs length
math.compatible(100px, 3rem)falseAbsolute vs relative

🛠 Compatibility

This is about Sass compilers, not browsers. The boolean is resolved while compiling—browsers never see a math.compatible() call.

Implementationmath.compatible()Notes
Dart Sass (modules)YesUse @use "sass:math" (about 1.23+)
Global comparable()Yes (legacy name)Same idea; prefer the module form in new code
LibSass / Ruby SassNo module APIMay still expose global comparable(); prefer Dart Sass

⚡ Quick Reference

GoalCode
Import math@use "sass:math";
Check unitsmath.compatible($a, $b)
Same unitmath.compatible(2px, 1px)true
Convertible lengthsmath.compatible(10cm, 3mm)true
Incompatible mixmath.compatible(100px, 3em)false
Guard before adding@if math.compatible($a, $b) { ... }
Legacy globalcomparable($a, $b)
Debug while learning@debug math.compatible(2px, 1px);

📋 compatible vs comparable

Same check, different names depending on how you load Sass math.

math.compatible()comparable()
Wheresass:math moduleGlobal built-in
Load with@use "sass:math"No import required
Modern style?Yes—preferredLegacy-friendly
BehaviorBoolean unit checkSame idea

📋 Why Other Math Functions Need Compatible Units

FunctionNeeds compatible units?How compatible helps
math.clamp()Yes (or all unitless)Check args before clamping
math.min() / math.max()Yes among numbersAvoid mixed px/em lists
math.hypot()Yes (or all unitless)Validate vector components
math.atan2()Yes (or unitless)Validate $y / $x pair

Those functions error when units clash. math.compatible() lets you detect the clash early and choose a fallback.

Examples Gallery

Each example uses @use "sass:math". Open View Compiled CSS to see what Dart Sass emits. Booleans often appear via interpolation or @if branches.

📚 Getting Started

Official-style checks from the Sass docs.

Example 1 — Basic math.compatible()

Same units are always compatible.

styles.scss
@use "sass:math";

@debug math.compatible(2px, 1px); // true

.demo {
  --same-unit: #{math.compatible(2px, 1px)};
}

How It Works

Both values use px, so the check is true. Interpolating with #{} writes the boolean into a custom property for learning or tooling.

Example 2 — True vs False Cases

Convertible lengths pass; absolute vs relative fails.

styles.scss
@use "sass:math";

@debug math.compatible(10cm, 3mm);  // true
@debug math.compatible(100px, 3em); // false

.checks {
  --cm-mm: #{math.compatible(10cm, 3mm)};
  --px-em: #{math.compatible(100px, 3em)};
}

How It Works

cm and mm share an absolute length family, so Sass can convert them. px and em cannot be converted at compile time without a known font size, so the check is false.

📈 Practical Patterns

Guard additions, wrap helpers, and know the legacy alias.

Example 3 — Guard Before Adding

Only add when units are compatible; otherwise keep a safe fallback.

styles.scss
@use "sass:math";

$base: 12px;
$extra: 0.5in; // compatible absolute length → 48px

.safe {
  @if math.compatible($base, $extra) {
    margin-left: $base + $extra;
  } @else {
    margin-left: $base;
  }
}

$bad: 2em;

.unsafe-fallback {
  @if math.compatible($base, $bad) {
    margin-left: $base + $bad;
  } @else {
    margin-left: $base; // px + em would error
  }
}

How It Works

0.5in converts to 48px, so 12px + 48px becomes 60px. The em branch fails the check, so the stylesheet keeps the plain 12px fallback instead of crashing the build.

Example 4 — Safe Offset Mixin

Reusable helper that refuses incompatible offsets.

styles.scss
@use "sass:math";

@mixin offset($from, $by) {
  @if math.compatible($from, $by) {
    transform: translateX($from + $by);
  } @else {
    transform: translateX($from);
  }
}

.card {
  @include offset(24px, 1cm); // 1cm ≈ 37.795px → ~61.795px
}

.chip {
  @include offset(24px, 1rem); // incompatible → keep 24px
}

How It Works

The mixin asks math.compatible() first. Convertible absolute lengths combine; relative units fall back to the base offset so the build stays green.

Example 5 — Module vs Global comparable()

Both names answer the same unit question.

styles.scss
@use "sass:math";

.names {
  --module: #{math.compatible(10cm, 3mm)};
  --global: #{comparable(10cm, 3mm)};
}

How It Works

Both calls return true for cm/mm. Prefer math.compatible() so readers see the check comes from sass:math.

🚀 Real-World Use Cases

  • Theme maps — validate user tokens before adding gaps or paddings.
  • Shared mixins — accept any length-like args and fail soft when units clash.
  • Design systems — assert that spacing scales stay in one unit family.
  • Pre-checks for clamp/min/max — avoid cryptic unit errors deeper in a stack.
  • Migration tooling — flag files that still mix px with em/rem.
  • Learning units — print true/false custom properties while exploring Sass units.

🧠 How Compilation Works

1

Write SCSS

Call math.compatible($a, $b) after @use.

Source
2

Compare unit families

Sass checks whether both numbers share a convertible unit system.

Validate
3

Return a boolean

true means safe to add/compare; false means stop or convert manually.

Result
4

Branches compile away

Your @if path ships only the CSS that matched.

⚠️ Common Pitfalls

  • Expecting conversioncompatible only returns a boolean; it does not rewrite units for you.
  • Mixing px and em/rem — almost always false; normalize the family first.
  • Percent with lengths10% and 20px are not compatible.
  • Putting the boolean in CSS blindly — prefer @if for real styles; use custom properties mainly for debugging.
  • Confusing the two names — module = compatible, global = comparable.

💡 Best Practices

✅ Do

  • Use @use "sass:math" and math.compatible() in new SCSS
  • Guard risky add/compare/clamp calls with @if
  • Keep design tokens inside one unit family when possible
  • Pair with clear fallbacks when units might clash
  • Prefer Dart Sass for the modern module API

❌ Don’t

  • Assume true means the numbers are equal—only that units match
  • Mix absolute and relative lengths without a plan
  • Rely on LibSass for sass:math
  • Ship debug custom properties in production unless you need them
  • Forget the global name is comparable, not compatible

Key Takeaways

Knowledge Unlocked

Five things to remember about math.compatible()

Boolean unit check—safe add/compare when true.

5
Core concepts
📦 02

Module

sass:math

@use
📏 03

Returns

boolean

true/false
04

Global

comparable()

Alias
05

Use

guard math

@if

❓ Frequently Asked Questions

math.compatible($number1, $number2) returns true when the two numbers have compatible units, and false otherwise. Unitless values are compatible with each other and with lengths. If it returns true, you can safely add, subtract, or compare them.
Add @use "sass:math"; then call math.compatible($a, $b). Example: math.compatible(2px, 1px) is true; math.compatible(100px, 3em) is false.
Official docs note that the global function is comparable($number1, $number2). When it moved into sass:math, the module name became compatible to describe the check more clearly. Prefer math.compatible() in new code.
No. It only answers yes or no. If the answer is true, other operations (like $a + $b or math.clamp) can convert compatible absolute units as needed.
Use it in mixins, maps, or theme helpers before you add or compare values that might mix unit families (for example px vs em). Guard with @if math.compatible(...) to avoid compile errors.
Built-in modules with @use need Dart Sass (about 1.23+). Older setups can still call the global comparable() name. LibSass and Ruby Sass do not load sass:math the same way.
Did you know?

Official Sass docs call out that the global name is comparable, but the sass:math name became compatible to state the purpose more clearly: do these two numbers share compatible units?

Conclusion

math.compatible() is a simple boolean safety net for Sass units. Call it before add/compare operations when inputs might mix families, remember the global alias is comparable(), and keep new code on @use "sass:math".

Continue with math.clamp(), math.cos(), or math.hypot().

More Sass Math

Continue with cosine in the math module.

math.cos() →

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