Sass math.unit()

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

Definition and Usage

math.unit($number) returns a quoted string describing $number’s units. Unitless values return "". Load it with @use "sass:math" on Dart Sass 1.23+.

Official samples.

math.unit(100) → "" · math.unit(100px) → "px" · math.unit(5px * 10px) → "px*px" · math.unit(math.div(5px, 1s)) → "px/s".

Debugging helper.

Official docs say this is intended for debugging. The string format is not guaranteed across Sass versions or implementations—do not build production design-system logic on the exact text.

Need a yes/no check?

Use math.is-unitless() when you only care whether units exist, not what they are.

Syntax

SCSS
@use "sass:math";

math.unit($number); //=> quoted string

math.unit(100);                 // ""
math.unit(100px);               // "px"
math.unit(5px * 10px);          // "px*px"
math.unit(math.div(5px, 1s));   // "px/s"

// Global alias (legacy): unit($number)
// Prefer math.unit after @use "sass:math"

Pattern: inspect a value while debugging → read the unit string → fix mismatches with math.compatible or math.div.

Unit Strings

How common numbers look:

InputMeaningResult
100Unitless""
100pxSimple length"px"
50%Percentage"%"
5px * 10pxArea-style compound"px*px"
math.div(5px, 1s)Rate"px/s"
Compound units.

Multiplication and division can produce strings like px*px or px/s. Treat them as debug output, not a stable public API.

SCSS Examples

Examples target Dart Sass with @use "sass:math". Official samples use @debug; examples emit via content so View Compiled CSS shows the quoted strings.

Example 1 — Official Docs Sample (Unitless)

Unitless numbers return an empty quoted string.

SCSS
@use "sass:math";

@debug math.unit(100); // ""

.basic {
  content: math.unit(100);
}

How It Works

  1. 100 has no CSS unit.
  2. math.unit returns "".
  3. content keeps the quoted empty string.
  4. Matches the official Sass docs sample.

Example 2 — Simple Length Unit

Official sample: 100px → "px".

SCSS
@use "sass:math";

@debug math.unit(100px); // "px"

.simple {
  content: math.unit(100px);
}

How It Works

  1. Input is a length with px.
  2. Return value is the quoted string "px".
  3. Useful when debugging unexpected unit mixes.
  4. Prefer @debug in real workflows; content is for this tutorial demo.

Example 3 — Compound Units (px*px)

Official sample: multiplying lengths compounds the unit string.

SCSS
@use "sass:math";

@debug math.unit(5px * 10px); // "px*px"

.compound {
  content: math.unit(5px * 10px);
}

How It Works

  1. 5px * 10px is 50px*px numerically.
  2. math.unit reports "px*px".
  3. Signal that you multiplied lengths instead of dividing to a ratio.
  4. Format may change across Sass versions—debug only.

Example 4 — Rate Units (px/s)

Official sample: division can leave a numerator/denominator unit string.

SCSS
@use "sass:math";

@debug math.unit(math.div(5px, 1s)); // "px/s"

.rate {
  content: math.unit(math.div(5px, 1s));
}

How It Works

  1. math.div(5px, 1s) cancels nothing shared.
  2. Result units become px/s.
  3. math.unit surfaces that string.
  4. Prefer math.div over / for division.

Example 5 — Pair with math.is-unitless

Boolean check first; print the unit string only when needed.

SCSS
@use "sass:math";

$value: 16rem;

.guard {
  --unitless: #{math.is-unitless($value)};
  content: math.unit($value);
}

How It Works

  1. is-unitless answers yes/no.
  2. unit answers which units.
  3. Together they clarify mixed-token bugs.
  4. Still treat the string as debug output.

Edge Cases & Pitfalls

Most unit() bugs are treating the string as a stable API or forgetting interpolation.

debug

Not a public contract

Official docs warn the format can change. Prefer booleans and math helpers for real logic.

vars

Custom properties need #{}

Writing --u: math.unit(100px); can pass the call through unevaluated. Interpolate or use a normal property like content.

boolean

Need only yes/no?

Use math.is-unitless instead of parsing the string.

global

Prefer the module form

Global unit() still exists for legacy code; new projects should use math.unit after @use.

Sass Support

Prefer the module form with Dart Sass 1.23+.

APIStatus
math.unit()Dart Sass 1.23+ via @use "sass:math"
Global unit()Legacy; prefer module form (globals deprecated in modern Dart Sass)
Return typeQuoted string
LibSass / Ruby SassNo modern sass:math module; global unit() may exist on older toolchains

Compile with the Dart Sass CLI.

Key Takeaways

  • Returns a quoted unit string (empty when unitless).
  • Debugging only — format is not a stable contract.
  • Shows compounds like px*px and px/s.
  • Pair with is-unitless / compatible for real guards.

One line: when you need to see which units a Sass number carries while debugging, call math.unit($number).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:math"
2. Call math.unit($number) while debugging
3. Expect a quoted string ("" if unitless)
4. Do not depend on the exact format in production
5. Prefer is-unitless / compatible for logic

Frequently Asked Questions

math.unit($number) returns a quoted string showing the number's units. Examples: math.unit(100) is ""; math.unit(100px) is "px"; math.unit(5px * 10px) is "px*px"; math.unit(math.div(5px, 1s)) is "px/s".
Add @use "sass:math"; then call math.unit($number). Prefer this module form in new SCSS.
Official Sass docs say it is intended for debugging. The output format is not guaranteed to stay the same across Sass versions or implementations, so avoid building critical design-system logic on the exact string.
The global built-in is unit($number). On sass:math the same helper is math.unit(). Prefer math.unit() in new code; global built-ins are deprecated in modern Dart Sass.
math.is-unitless($n) returns a boolean (has units or not). math.unit($n) returns the unit string itself, which is useful when you want to see px, %, px*px, or px/s while debugging.
Built-in modules with @use need Dart Sass (about 1.23+). Older setups can still call the global unit() name. LibSass and Ruby Sass do not load sass:math the same way.

Next: Sass color.adjust()

Move on from math helpers to adjusting color channels.

color.adjust() →

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