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
Concept
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(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.
Units are compatible (including unitless with lengths). Safe to add, subtract, or compare.
false
Units are incompatible. Adding or comparing them will error.
Modules
📦 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().
Numbers
📏 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.
Call
Result
Notes
math.compatible(2px, 1px)
true
Same unit (official example)
math.compatible(10cm, 3mm)
true
Convertible absolute lengths
math.compatible(100px, 3em)
false
Absolute vs relative
math.compatible(1, 2)
true
Both unitless
math.compatible(1, 1px)
true
Unitless works with lengths
math.compatible(1deg, 1rad)
true
Compatible angles
math.compatible(1s, 500ms)
true
Compatible times
math.compatible(10%, 20px)
false
Percent vs length
math.compatible(100px, 3rem)
false
Absolute vs relative
Support
🛠 Compatibility
This is about Sass compilers, not browsers. The boolean is resolved while compiling—browsers never see a math.compatible() call.
Implementation
math.compatible()
Notes
Dart Sass (modules)
Yes
Use @use "sass:math" (about 1.23+)
Global comparable()
Yes (legacy name)
Same idea; prefer the module form in new code
LibSass / Ruby Sass
No module API
May still expose global comparable(); prefer Dart Sass
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import math
@use "sass:math";
Check units
math.compatible($a, $b)
Same unit
math.compatible(2px, 1px) → true
Convertible lengths
math.compatible(10cm, 3mm) → true
Incompatible mix
math.compatible(100px, 3em) → false
Guard before adding
@if math.compatible($a, $b) { ... }
Legacy global
comparable($a, $b)
Debug while learning
@debug math.compatible(2px, 1px);
Compare
📋 compatible vs comparable
Same check, different names depending on how you load Sass math.
math.compatible()
comparable()
Where
sass:math module
Global built-in
Load with
@use "sass:math"
No import required
Modern style?
Yes—preferred
Legacy-friendly
Behavior
Boolean unit check
Same idea
Compare
📋 Why Other Math Functions Need Compatible Units
Function
Needs compatible units?
How compatible helps
math.clamp()
Yes (or all unitless)
Check args before clamping
math.min() / math.max()
Yes among numbers
Avoid 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.
Hands-On
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.
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.
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.
Both calls return true for cm/mm. Prefer math.compatible() so readers see the check comes from sass:math.
Applications
🚀 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.
Watch Out
⚠️ Common Pitfalls
Expecting conversion — compatible 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 lengths — 10% 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.
Pro Tips
💡 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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about math.compatible()
Boolean unit check—safe add/compare when true.
5
Core concepts
📝01
Call
compatible(a, b)
API
📦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?
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".