Sass math.compatible()

Beginner
8 min read
Updated: Nov 2026
5 examples
Dart Sass 1.23+

Definition and Usage

math.compatible($number1, $number2) returns whether the two numbers have compatible units. If it returns true, they can safely be added, subtracted, or compared. Otherwise those operations will error. Load it with @use "sass:math" (Dart Sass 1.23+ for modules).

Official samples.

math.compatible(2px, 1px) → true · math.compatible(100px, 3em) → false · math.compatible(10cm, 3mm) → true.

Global name is comparable().

Official docs: the global helper stayed comparable, but the module renamed it to compatible to say what it checks. Prefer math.compatible; global built-ins are deprecated in modern Dart Sass.

Boolean only — no conversion.

This function does not change units. It answers yes/no so your mixins can branch before +, -, comparisons, or math.clamp.

Syntax

SCSS
@use "sass:math";

math.compatible($number1, $number2); //=> boolean

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

// Global name (legacy / deprecated in modern Dart Sass)
comparable(2px, 1px); // true

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

Pattern: inspect tokens → branch with @if → only then add or compare. Inspect units with math.unit and math.is-unitless.

When Units Match

Common pairs and their verified results:

CallResult
math.compatible(2px, 1px)true
math.compatible(10cm, 3mm)true (compatible absolutes)
math.compatible(100px, 3em)false
math.compatible(10, 3)true (both unitless)
math.compatible(10, 3px)true (unitless with length)
math.compatible(10%, 3%)true
What “compatible” unlocks.

When this returns true, Sass can add, subtract, and compare the values. Absolute lengths like cm/mm may convert during those operations.

SCSS Examples

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

Example 1 — Official Docs Sample (Same Unit)

Matching length units are compatible.

SCSS
@use "sass:math";

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

.basic {
  --a: #{math.compatible(2px, 1px)};
}

How It Works

  1. Both args are px lengths.
  2. Compatible → true.
  3. Safe to add or compare them later.
  4. Matches the official Sass docs sample.

Example 2 — Official Docs Sample (px vs em)

Relative and absolute length families do not mix.

SCSS
@use "sass:math";

@debug math.compatible(100px, 3em); // false

.fail {
  --b: #{math.compatible(100px, 3em)};
}

How It Works

  1. px is absolute; em is relative.
  2. Sass cannot convert between them at compile time.
  3. Result is false.
  4. Adding them would be a compile error.

Example 3 — Compatible Absolute Lengths

Official sample: cm and mm share a unit family.

SCSS
@use "sass:math";

@debug math.compatible(10cm, 3mm); // true

.convert {
  --c: #{math.compatible(10cm, 3mm)};
}

How It Works

  1. Absolute lengths convert among themselves.
  2. cm / mm are compatible → true.
  3. Later ops (like clamp or +) may convert units.
  4. Matches the official Sass docs sample.

Example 4 — Unitless and Same-Family Mixes

Unitless numbers play nicely with lengths; percentages match percentages.

SCSS
@use "sass:math";

.more {
  --unitless: #{math.compatible(10, 3)};
  --mixed: #{math.compatible(10, 3px)};
  --pct: #{math.compatible(10%, 3%)};
}

How It Works

  1. Two unitless values → true.
  2. Unitless + length is also compatible in Sass.
  3. Same % units → true.
  4. Still check before mixing unknown theme tokens.

Example 5 — Guard Addition with @if

Skip an unsafe sum when units disagree.

SCSS
@use "sass:math";

$a: 12px;
$b: 1rem;

.gate {
  @if math.compatible($a, $b) {
    width: $a + $b;
  } @else {
    width: $a;
  }
}

How It Works

  1. px and rem are not compatible.
  2. @if takes the false branch.
  3. Output uses $a alone — no compile error.
  4. Useful in shared mixins that accept unknown tokens.

Edge Cases & Pitfalls

Most compatible bugs are assuming a yes means units are identical, or calling the deprecated global name.

name

Module vs global naming

Module: math.compatible. Global: comparable (deprecated in modern Dart Sass).

convert

Does not rewrite units

A true result only unlocks later math; it does not return a converted number.

relative

px vs em/rem

These fail compatibility checks. Normalize tokens in one unit family first.

debug

Inspect with math.unit

When a check surprises you, print units with math.unit.

Sass Support

Prefer the module form with Dart Sass 1.23+.

APIStatus
math.compatible()Dart Sass 1.23+ via @use "sass:math"
Global comparable()Works; deprecated in modern Dart Sass
Return typeBoolean
LibSass / Ruby SassUse global comparable() if modules are unavailable

Compile with the Dart Sass CLI.

Key Takeaways

  • Returns a boolean for unit compatibility.
  • true unlocks add, subtract, and compare.
  • Module name is compatible; global alias is comparable.
  • Prefer math.compatible over deprecated global comparable().

One line: before adding or comparing uncertain Sass numbers, call math.compatible($a, $b).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:math"
2. Call math.compatible($a, $b)
3. true → safe to add / subtract / compare
4. false → normalize units or pick a fallback
5. Prefer math.compatible over global comparable()

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, but modern Dart Sass deprecates global built-ins. Prefer math.compatible().

Next: Sass math.cos()

Return the cosine of an angle in Sass.

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.

8 people found this page helpful