Sass math.abs()

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

Definition and Usage

math.abs($number) returns the absolute value of $number. If $number is negative, this returns -$number; if positive, it returns $number as-is. Load it with @use "sass:math" on Dart Sass 1.23+. Global abs() still works.

Official samples.

math.abs(10px) → 10px. math.abs(-10px) → 10px.

Units are kept.

Only the sign changes. math.abs(-1.25rem) is 1.25rem; percentages and unitless numbers work the same way.

Compile-time result.

Sass resolves math.abs while compiling. The CSS file gets a plain positive number—not a live abs() call (unless you write CSS abs() yourself).

Syntax

SCSS
@use "sass:math";

math.abs($number); //=> number

math.abs(10px);   // 10px
math.abs(-10px);  // 10px
math.abs(-4.5);   // 4.5
math.abs(-25%);   // 25%

// Legacy global (prefer module form)
// abs(-10px);

Pattern: compute a signed length or delta → math.abs when you need a positive CSS length → pair with math.clamp or math.min / math.max for ranges.

Units and Signs

Absolute value only flips the sign; everything else stays:

InputResult
math.abs(10px)10px
math.abs(-10px)10px
math.abs(0)0
math.abs(-4.5)4.5 (unitless)
math.abs(-25%)25%
Sass vs CSS abs()Compile time vs runtime in the browser
Distance helper family.

Official docs place math.abs with other distance helpers such as math.hypot.

SCSS Examples

Examples target Dart Sass 1.23+ with @use "sass:math". Open View Compiled CSS for verified output (matches official Sass docs samples).

Example 1 — Positive and Negative (Official Docs)

Negatives flip; positives stay the same.

SCSS
@use "sass:math";

@debug math.abs(10px);  // 10px
@debug math.abs(-10px); // 10px

.basic {
  --pos: #{math.abs(10px)};
  --neg: #{math.abs(-10px)};
}

How It Works

  1. Positive input returns unchanged.
  2. Negative input returns the positive magnitude.
  3. px unit is preserved either way.
  4. Matches the official Sass docs sample.

Example 2 — Keep Common CSS Units

Works with rem, %, and unitless numbers.

SCSS
@use "sass:math";

.units {
  --rem: #{math.abs(-1.25rem)};
  --pct: #{math.abs(-25%)};
  --unitless: #{math.abs(-4.5)};
}

How It Works

  1. Sign flips; unit string stays.
  2. Percentages remain percentages.
  3. Unitless numbers stay unitless.
  4. Useful for token math that can go negative.

Example 3 — Positive Lengths from Signed Tokens

Store signed design tokens, emit positive CSS lengths.

SCSS
@use "sass:math";

$offset: -12px;

.layout {
  margin-left: math.abs($offset);
  width: math.abs(-40%);
}

How It Works

  1. Signed token can represent direction in Sass.
  2. math.abs produces a CSS-safe length.
  3. Compiled CSS has no negative margin/width here.
  4. Keep the signed value if you still need direction elsewhere.

Example 4 — Absolute Difference

Subtract two lengths, then take the magnitude.

SCSS
@use "sass:math";

$delta: 20px - 48px; // -28px

.gap {
  --raw: #{$delta};
  --abs: #{math.abs($delta)};
  padding: math.abs($delta);
}

How It Works

  1. Subtraction can yield a negative length.
  2. math.abs turns that into a positive gap.
  3. Raw delta can still be inspected for debugging.
  4. Common pattern for spacing from two measured sizes.

Example 5 — Zero Stays Zero

Absolute value of zero is zero.

SCSS
@use "sass:math";

.zero {
  --z: #{math.abs(0)};
  --zp: #{math.abs(0px)};
}

How It Works

  1. Zero is non-negative, so abs leaves it alone.
  2. Unitless 0 and 0px both work.
  3. Safe default when a calculation may cancel out.
  4. No special case needed in your own wrappers.

Edge Cases & Pitfalls

Most abs bugs are expecting a browser-time function, or stripping units by mistake.

units

Units are preserved

math.abs does not convert px to unitless. Only the sign changes.

stage

Compile time ≠ CSS abs()

Need a value that reacts in the browser? Use CSS abs() (where supported). Need a fixed number in the stylesheet? Use Sass math.abs.

direction

Abs removes direction

If a negative length means “shift left,” store the signed value separately before calling abs.

legacy

Prefer math.abs over abs

Global abs() still works, but new code should use the sass:math module.

Sass Support

Prefer the module form with a current Dart Sass release.

APIStatus
math.abs()Dart Sass 1.23+ via @use "sass:math"
Global abs()Available; prefer module form in new code
Return typeNumber (same units as input)
LibSass / Ruby SassNo modern sass:math module—use global abs() if needed

Compile with the Dart Sass CLI.

Key Takeaways

  • Returns the magnitude of a number; negatives become positive.
  • Keeps CSS units such as px, rem, and %.
  • Resolved at compile time into a plain CSS number.
  • Prefer math.abs after @use "sass:math".

One line: when a Sass number must be non-negative in CSS, call math.abs($number).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:math"
2. Call math.abs on signed lengths / deltas
3. Expect units to stay on the result
4. Use CSS abs() only when you need runtime behavior
5. Prefer math.abs over global abs() in new code

Frequently Asked Questions

math.abs($number) returns the absolute value of a Sass number. If $number is negative, it returns -$number; if positive, it returns $number as-is. 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 1.23+. LibSass and Ruby Sass do not load sass:math the same way—use the global abs() name there if you must.

Next: Sass math.acos()

Return the arccosine of a unitless number in degrees.

math.acos() →

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