Sass math.abs() Function

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:math · Dart Sass

What You’ll Learn

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

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.

📝 Syntax

Load the math module, then call the function:

styles.scss
@use "sass:math";

math.abs($number)

Parameters

ParameterTypeRequiredDescription
$numberNumberYesAny Sass number, with or without a unit (for example -12, 4.5, -24px, 10%).

Return value

TypeSituationResult
Number$number is negative-$number (positive counterpart; unit kept)
Number$number is positive$number unchanged
Number$number is zero0 (same unit if present)

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

📏 How Units Work

math.abs() changes the sign only. The unit travels with the number.

InputOutputNotes
math.abs(-10px)10pxLength unit kept
math.abs(-1.5rem)1.5remRelative unit kept
math.abs(-25%)25%Percentage kept
math.abs(-8)8Unitless stays unitless
math.abs(12px)12pxAlready positive

🛠 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 — preferredYes
LibSassNo built-in modulesYes (legacy)
Ruby SassNo built-in modulesYes (legacy)

Prefer Dart Sass for new work. If you maintain an old LibSass pipeline, abs($number) without a module may still be available.

⚡ Quick Reference

GoalCode
Import math@use "sass:math";
Absolute valuemath.abs($n)
Negative length → positivemath.abs(-24px)24px
Use in a propertymargin-left: math.abs($offset);
Legacy global callabs($n)
Debug while learning@debug math.abs(-10px);

📋 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 runsCompile time (Dart Sass)In the browser
Appears in CSS output?No — only the resultYes — as abs(...)
Needs modern browser support?NoYes (CSS Values)
Best forFixed design tokens & SCSS mathRuntime / dynamic values

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.

styles.scss
@use "sass:math";

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

.box {
  width: math.abs(-120px);
  height: math.abs(80px);
}

How It Works

Sass evaluates both calls while compiling. The stylesheet only contains 120px and 80px.

Example 2 — Units Stay Attached

Absolute value does not strip rem or %.

styles.scss
@use "sass:math";

$shift: -1.5rem;
$share: -25%;

.card {
  padding-inline-start: math.abs($shift);
  flex-basis: math.abs($share);
}

How It Works

Only the minus sign is removed. Design tokens can keep their units intact.

📈 Practical Patterns

Offsets, combined math, and loops that cross zero.

Example 3 — Safe Offset for Positioning

Keep a signed token, emit non-negative insets.

styles.scss
@use "sass:math";

$nudge: -18px;

.badge {
  position: absolute;
  top: math.abs($nudge);
  right: math.abs($nudge);
}

How It Works

$nudge can stay signed for other calculations, while the badge always gets a positive distance from the edges.

Example 4 — Absolute Value in Calculations

Combine abs with addition so progress width never goes negative.

styles.scss
@use "sass:math";

$delta: -15;
$base: 10;
$progress: math.abs($delta) + $base; // 25

.progress-bar {
  width: $progress * 1%;
}

How It Works

math.abs(-15) becomes 15, then + 10 yields 25, scaled to a percentage.

Example 5 — Absolute Spacing in a @for Loop

Loop indexes can be negative; spacing usually should not be.

styles.scss
@use "sass:math";

@for $i from -3 through 3 {
  .item-#{$i} {
    margin-left: math.abs($i) * 10px;
  }
}

How It Works

For $i = -3, math.abs(-3) * 10px is 30px—the same distance as for $i = 3.

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

⚠️ Common Pitfalls

  • Forgetting @usemath.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.

💡 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

Key Takeaways

Knowledge Unlocked

Five things to remember about math.abs()

Compile-time absolute value—units included.

5
Core concepts
📦 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.

Conclusion

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.

More Sass Basics

Return to the introduction for variables, nesting, and mixins.

Sass introduction →

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.

6 people found this page helpful