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
Concept
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.
Foundation
📝 Syntax
Load the math module, then call the function:
styles.scss
@use "sass:math";
math.unit($number)
Parameters
Parameter
Type
Required
Description
$number
Number
Yes
The Sass number whose units you want to inspect.
Return value
Type
Meaning
Quoted string
A text description of the units (for example "px", "%", "px*px").
""
Empty quoted string when the number is unitless.
Modules
📦 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.
Numbers
📏 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.
Call
Result
Notes
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
Support
🛠 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.
Implementation
math.unit()
Notes
Dart Sass (modules)
Yes
Use @use "sass:math" (about 1.23+)
Global unit()
Yes (legacy name)
Same idea; prefer the module form in new code
LibSass / Ruby Sass
No module API
May still expose global unit(); prefer Dart Sass
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import math
@use "sass:math";
Read units
math.unit($n)
Unitless value
math.unit(100) → ""
Simple length
math.unit(100px) → "px"
Compound product
math.unit(5px * 10px) → "px*px"
Compound quotient
math.unit(math.div(5px, 1s)) → "px/s"
Debug while compiling
@debug math.unit($n);
Legacy global
unit($n)
Compare
📋 unit vs is-unitless vs compatible
Three unit helpers, three different questions.
Helper
Returns
Question it answers
math.unit($n)
Quoted string
What units does this number have?
math.is-unitless($n)
Boolean
Does this number have no units?
math.compatible($a, $b)
Boolean
Can 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.
Guidance
📋 Debugging vs Production Logic
Use
Good fit?
Why
@debug math.unit($n)
Yes
Official learning / inspection path
Temporary custom properties
Okay while learning
Helps visualize units in DevTools
Hard-coded string equals in a theme API
Risky
Format is not a stable public contract
Guarding log/pow/sqrt
Prefer is-unitless
Boolean intent is clearer and more stable
Hands-On
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.
Use string equals when a boolean helper already answers the question
Rely on LibSass for sass:math
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about math.unit()
Quoted unit string—best for debugging and learning.
5
Core concepts
📝01
Call
unit($number)
API
📦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.
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".