Sass math.unit() Function

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

What You’ll Learn

math.unit() is a unit helper in the sass:math module. It turns a number’s units into a quoted string you can @debug, compare while learning, or print into CSS for inspection. This page covers empty strings, compound units, the global unit() alias, the official debugging caveat, and five compiled examples.

01

Concept

Unit string

02

Module

@use "sass:math"

03

Returns

Quoted string

04

Global

unit()

05

Best for

Debugging

06

Practice

5 examples

What Is math.unit()?

Every Sass number has a magnitude and optional units. math.unit($number) returns a quoted string that describes those units. Official docs place it under Unit Functions next to math.is-unitless() and math.compatible().

  • math.unit(100)"" (unitless)
  • math.unit(100px)"px"
  • math.unit(5px * 10px)"px*px"
  • math.unit(math.div(5px, 1s))"px/s"
⚠️
Heads up from the docs

Sass documents math.unit() as a debugging helper. The exact string format is not guaranteed across Sass versions or implementations—great for learning and @debug, risky as a permanent API contract in a design system.

📝 Syntax

Load the math module, then call the function:

styles.scss
@use "sass:math";

math.unit($number)

Parameters

ParameterTypeRequiredDescription
$numberNumberYesThe Sass number whose units you want to inspect.

Return value

TypeMeaning
Quoted stringA text description of the units (for example "px", "%", "px*px").
""Empty quoted string when the number is unitless.

📦 Loading sass:math

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

styles.scss
// Recommended — namespaced
@use "sass:math";
$u: math.unit(100px);

// Optional — bring members into the current namespace
@use "sass:math" as *;
$u: unit(100px); // math module member

// Optional — custom namespace
@use "sass:math" as m;
$u: m.unit(100px);

// Legacy global name (still works in many setups)
$u: unit(100px);
💡
Name tip

Module and global forms share the short name unit. New SCSS should still load sass:math and call math.unit() so the source of the helper is obvious.

📏 What the String Looks Like

Simple units print as themselves. Multiplying lengths builds compound units with *. Dividing units builds a numerator/denominator form with /. Canceling matching units (for example px / px) leaves an empty string.

CallResultNotes
math.unit(100)""Unitless (official example)
math.unit(100px)"px"Official example
math.unit(5px * 10px)"px*px"Compound product
math.unit(math.div(5px, 1s))"px/s"Compound quotient
math.unit(10%)"%"Percent is a unit
math.unit(1deg)"deg"Angle unit
math.unit(math.div(24px, 16px))""Matching units cancel

🛠 Compatibility

This is about Sass compilers, not browsers. The string is produced while compiling—browsers never see a math.unit() call unless you interpolate the result into CSS on purpose.

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

⚡ Quick Reference

GoalCode
Import math@use "sass:math";
Read unitsmath.unit($n)
Unitless valuemath.unit(100)""
Simple lengthmath.unit(100px)"px"
Compound productmath.unit(5px * 10px)"px*px"
Compound quotientmath.unit(math.div(5px, 1s))"px/s"
Debug while compiling@debug math.unit($n);
Legacy globalunit($n)

📋 unit vs is-unitless vs compatible

Three unit helpers, three different questions.

HelperReturnsQuestion it answers
math.unit($n)Quoted stringWhat units does this number have?
math.is-unitless($n)BooleanDoes this number have no units?
math.compatible($a, $b)BooleanCan these two numbers be added or compared?

Reach for math.unit() when you want to see the units. Reach for is-unitless / compatible when you need a yes/no branch in real stylesheets.

📋 Debugging vs Production Logic

UseGood fit?Why
@debug math.unit($n)YesOfficial learning / inspection path
Temporary custom propertiesOkay while learningHelps visualize units in DevTools
Hard-coded string equals in a theme APIRiskyFormat is not a stable public contract
Guarding log/pow/sqrtPrefer is-unitlessBoolean intent is clearer and more stable

Examples Gallery

Each example uses @use "sass:math". Open View Compiled CSS to see what Dart Sass emits.

📚 Getting Started

Official-style unit strings from the Sass docs.

Example 1 — Basic math.unit()

Unitless numbers return an empty quoted string; lengths return the unit name.

styles.scss
@use "sass:math";

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

.demo {
  --empty: "#{math.unit(100)}";
  --px: #{math.unit(100px)};
}

How It Works

Wrapping the empty result in quotes keeps "" visible in CSS. Interpolating "px" into a custom property usually lands as the unquoted identifier px.

Example 2 — Compound Units

Products and quotients keep both sides of the unit math.

styles.scss
@use "sass:math";

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

.physics {
  --area: #{math.unit(5px * 10px)};
  --speed: #{math.unit(math.div(5px, 1s))};
}

How It Works

Multiplying lengths stacks the unit with *. Dividing by time builds a px/s-style string—handy when you are tracing intermediate Sass math.

📈 Practical Patterns

Canceled units, inspection helpers, and the global alias.

Example 3 — Units That Cancel

After matching units cancel, math.unit() returns an empty string.

styles.scss
@use "sass:math";

$ratio: math.div(24px, 16px); // 1.5

.scale {
  --units: "#{math.unit($ratio)}";
  --is-unitless: #{math.is-unitless($ratio)};
  opacity: $ratio;
}

How It Works

px cancels, so the unit string is empty and math.is-unitless agrees with true.

Example 4 — Inspect Mixin for Learning

A temporary helper that writes the unit string into content.

styles.scss
@use "sass:math";

@mixin show-unit($value) {
  &::after {
    content: math.unit($value);
  }
}

.box {
  width: 18px;
  @include show-unit(18px);
}

.chip {
  @include show-unit(10%);
}

How It Works

Because math.unit() returns a quoted string, it fits naturally in content. Use patterns like this while learning—remove them before shipping.

Example 5 — Module vs Global unit()

Both names produce the same quoted unit string.

styles.scss
@use "sass:math";

.names {
  --module: #{math.unit(100px)};
  --global: #{unit(1deg)};
  --empty: "#{math.unit(42)}";
}

How It Works

Module and global forms agree. Prefer math.unit() so readers see the helper comes from sass:math.

🚀 Real-World Use Cases

  • @debug while learning — print units for confusing intermediate values.
  • Compound math tracing — spot unexpected px*px or px/s results.
  • Teaching units — show how division cancels units into "".
  • Temporary DevTools labels — write unit strings into custom properties while exploring.
  • Pairing with is-unitless — confirm both the boolean and the string during refactors.
  • Migration audits — quickly see which tokens still carry em, rem, or %.

🧠 How Compilation Works

1

Write SCSS

Call math.unit($n) after @use.

Source
2

Read the unit bag

Sass inspects numerator and denominator units on the number.

Inspect
3

Build a quoted string

Simple, *, or / forms—or "" when unitless.

Result
4

Use or discard

@debug it, interpolate it, or keep it out of production CSS.

⚠️ Common Pitfalls

  • Treating the string as a stable API — docs warn the format may change across versions.
  • Expecting a boolean — use math.is-unitless() for yes/no checks.
  • Empty string surprises — unitless values return "", not "none" or null.
  • Interpolating without quotes — empty units can disappear visually; wrap with "#{...}" when debugging.
  • Building production branches on exact matches — prefer compatible / is-unitless for real logic.

💡 Best Practices

✅ Do

  • Use @use "sass:math" and math.unit() in new SCSS
  • Reach for @debug first when exploring units
  • Pair with is-unitless and compatible for real guards
  • Remember compound forms like px*px and px/s
  • Prefer Dart Sass for the modern module API

❌ Don’t

  • Depend on the exact string format in long-lived theme APIs
  • Confuse unit() with CSS unit keywords in other languages
  • Ship temporary content / custom-property inspectors
  • Use string equals when a boolean helper already answers the question
  • Rely on LibSass for sass:math

Key Takeaways

Knowledge Unlocked

Five things to remember about math.unit()

Quoted unit string—best for debugging and learning.

5
Core concepts
📦 02

Module

sass:math

@use
📏 03

Returns

quoted string

"px"
04

Empty

"" if unitless

Edge
05

Use

debug / learn

Docs

❓ 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.
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.
Did you know?

Official Sass docs list math.unit under Unit Functions and warn that its string format is for debugging—not a guaranteed cross-version contract. That makes it perfect for learning compound units like px*px and px/s.

Conclusion

math.unit() returns a quoted string describing a number’s units—from empty "" to compound forms like "px*px". Use it to learn and debug, keep production guards on is-unitless / compatible, and prefer @use "sass:math".

Continue with color.adjust(), math.is-unitless(), or math.compatible().

Explore color.adjust() Next

Shift colors by fixed channel amounts with the sass:color module.

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

5 people found this page helpful