math.abs() belongs to the built-in sass:math module. It turns any number into its non-negative counterpart while keeping CSS units. This page walks through module loading, units, compiler support, Sass vs CSS abs(), and five compiled examples.
01
Concept
Absolute value
02
Module
@use "sass:math"
03
Units
Preserved
04
When
Compile time
05
Vs CSS
abs() in CSS
06
Practice
5 examples
Concept
What Is math.abs()?
In math, the absolute value of a number is its distance from zero. So |-7| = 7 and |7| = 7. Sass gives you the same idea while compiling stylesheets:
math.abs(-10px) → 10px
math.abs(10px) → 10px
math.abs(0) → 0
Many CSS properties expect non-negative lengths (width, padding, gap, and so on). When a token or formula might be negative—subtraction, a signed design token, a loop index—math.abs() makes the value safe before CSS is written.
💡
Beginner tip
Browsers never see math.abs. Dart Sass evaluates it and writes the final number into your .css file.
Foundation
📝 Syntax
Load the math module, then call the function:
styles.scss
@use "sass:math";
math.abs($number)
Parameters
Parameter
Type
Required
Description
$number
Number
Yes
Any Sass number, with or without a unit (for example -12, 4.5, -24px, 10%).
Return value
Type
Situation
Result
Number
$number is negative
-$number (positive counterpart; unit kept)
Number
$number is positive
$number unchanged
Number
$number is zero
0 (same unit if present)
Modules
📦 Loading sass:math
Modern Sass groups built-in helpers into modules. You load them with @use instead of relying on global names.
styles.scss
// Recommended — namespaced
@use "sass:math";
$w: math.abs(-40px);
// Optional — bring members into the current namespace
@use "sass:math" as *;
$w: abs(-40px); // still the math module member
// Optional — custom namespace
@use "sass:math" as m;
$w: m.abs(-40px);
⚠️
Put @use first
@use rules must appear before most other rules (except @forward and a few charset-style exceptions). Keep @use "sass:math"; near the top of the file.
Numbers
📏 How Units Work
math.abs() changes the sign only. The unit travels with the number.
Input
Output
Notes
math.abs(-10px)
10px
Length unit kept
math.abs(-1.5rem)
1.5rem
Relative unit kept
math.abs(-25%)
25%
Percentage kept
math.abs(-8)
8
Unitless stays unitless
math.abs(12px)
12px
Already positive
Support
🛠 Compatibility
This is about Sass compilers, not browsers. Compiled CSS is always plain numbers.
Implementation
@use "sass:math"
Global abs()
Dart Sass (1.23+)
Yes — preferred
Yes
LibSass
No built-in modules
Yes (legacy)
Ruby Sass
No built-in modules
Yes (legacy)
Prefer Dart Sass for new work. If you maintain an old LibSass pipeline, abs($number) without a module may still be available.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import math
@use "sass:math";
Absolute value
math.abs($n)
Negative length → positive
math.abs(-24px) → 24px
Use in a property
margin-left: math.abs($offset);
Legacy global call
abs($n)
Debug while learning
@debug math.abs(-10px);
Compare
📋 Sass math.abs() vs CSS abs()
Both compute absolute value. They run at different times in the pipeline.
Sass math.abs()
CSS abs()
When it runs
Compile time (Dart Sass)
In the browser
Appears in CSS output?
No — only the result
Yes — as abs(...)
Needs modern browser support?
No
Yes (CSS Values)
Best for
Fixed design tokens & SCSS math
Runtime / dynamic values
Hands-On
Examples Gallery
Each example uses @use "sass:math". Open View Compiled CSS to see what Dart Sass emits.
📚 Getting Started
Core absolute-value behavior with CSS units.
Example 1 — Basic math.abs()
Matches the official docs: positive stays positive; negative flips sign.
For $i = -3, math.abs(-3) * 10px is 30px—the same distance as for $i = 3.
Applications
🚀 Real-World Use Cases
Guaranteed positive lengths — width, height, padding, and gap from signed math.
Offset tokens — keep a signed design token; emit non-negative insets.
Progress / scale math — turn a signed delta into a positive share of a bar.
Loops that cross zero — mirror spacing on both sides of an index.
Distance between values — when only magnitude matters, not direction.
First math-module skill — a simple entry point into sass:math.
🧠 How Compilation Works
1
Write SCSS
Add @use "sass:math"; and call math.abs($n).
Source
2
Dart Sass runs
The compiler loads the math module and evaluates the call.
Compile
3
Sign is resolved
Negatives become positive; units stay on the number.
Abs
4
✓
Plain CSS ships
The browser only receives the final positive length or number.
Watch Out
⚠️ Common Pitfalls
Forgetting @use — math.abs is undefined until you load sass:math.
Expecting runtime behavior — changing a CSS custom property later will not re-run Sass math.abs.
Using abs to hide bad formulas — if the sign is wrong, fix the math when you can; do not only mask it.
LibSass pipelines — built-in modules need Dart Sass; migrate or use global abs() carefully.
Confusing with CSS abs() — leaving abs() in CSS output is a different feature with different support.
Pro Tips
💡 Best Practices
✅ Do
Use @use "sass:math" and math.abs() in new SCSS
Apply it when a formula might produce a negative length
Keep units on variables; let abs preserve them
Check results with @debug while learning
Prefer Dart Sass for built-in modules
❌ Don’t
Expect the browser to evaluate math.abs
Strip units before calling abs without a reason
Rely on LibSass for @use "sass:math"
Mix Sass abs and CSS abs without knowing which layer runs
Use abs as a substitute for clear, correct formulas
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about math.abs()
Compile-time absolute value—units included.
5
Core concepts
📝01
Call
math.abs($n)
API
📦02
Module
sass:math
@use
📏03
Units
preserved
CSS
⚡04
When
compile time
Sass
✓05
Prefer
Dart Sass
Tooling
❓ Frequently Asked Questions
math.abs($number) returns the absolute value of a Sass number. Negatives become positive; positives and zero stay the same. CSS units such as px, rem, and % are kept.
Add @use "sass:math"; at the top of your SCSS file, then call math.abs($number). Example: math.abs(-10px) compiles to 10px.
Yes. The global abs($number) name still works for older stylesheets. New projects should prefer math.abs() so the helper clearly comes from sass:math.
No. math.abs(-24px) is 24px. Only the sign changes. Unitless inputs stay unitless.
Same idea, different stage. Sass math.abs() runs when you compile SCSS. CSS abs() runs in the browser. Use Sass when the value must be fixed in the output CSS.
Built-in modules with @use need Dart Sass (about 1.23+). LibSass and Ruby Sass do not load sass:math the same way—use the global abs() name there if you must.
Did you know?
In the official Sass docs, math.abs sits under Distance Functions next to math.hypot. Absolute value asks “how far from zero?” in one dimension; hypot answers the same question across several lengths.
math.abs() is a small, reliable compile-time helper: feed it a number, get back a non-negative value with the same unit. Load it through sass:math, prefer Dart Sass, and use it whenever stylesheet math might dip below zero but your CSS needs a safe distance or size.
Continue with the Sass introduction or explore CSS topics to connect preprocessor math with the properties browsers apply.