Sass Numeric Operators

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

Definition and Usage

Sass supports standard math for numbers: +, -, *, and % (modulo). Compatible units convert automatically. Prefer math.div() for division.

Official sample idea.

10s + 15s is 25s; 100px + 50 is 150px (unitless mixes in).

Divide with math.div.

CSS uses / as a separator (font: 15px/32px). / as Sass division is deprecated—use @use "sass:math" and math.div().

Stay unit-clean.

Avoid #{$n}px—that builds a string, not a number. Prefer $n * 1px or give $n a unit from the start.

Syntax

SCSS
@use "sass:math";

$a + $b    // add
$a - $b    // subtract
$a * $b    // multiply
$a % $b    // modulo (remainder)
math.div($a, $b)  // divide (preferred)

10s + 15s;       // 25s
100px + 50;      // 150px
-(50px + 30px);  // -80px

// Spaces matter around - :
5px - 3px;       // subtraction
-$number;        // unary negation

Pattern: keep units on your numbers → compute with operators / math.div → emit CSS lengths and times.

Units & Division

How units and / behave:

CaseResult
Compatible units (1in + 6px)Converted and combined
Unitless + unit (100px + 50)Allowed — unitless adopts the unit
Incompatible (+ / - / %)Compile error (e.g. 100px + 10s)
* / math.div across unitsMay produce complex units (often as calc())
Literal 15px / 30pxOften a slash-separated value, not division

SCSS Examples

Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).

Example 1 — Official + - * %

Add times, subtract convertible lengths, multiply units, and take modulo.

SCSS
@use "sass:meta";

.probe {
  --a: #{meta.inspect(10s + 15s)};
  --b: #{meta.inspect(1in - 10px)};
  --c: #{meta.inspect(5px * 3px)};
  --d: #{meta.inspect(1in % 9px)};
}

How It Works

  1. + adds matching time units.
  2. - converts inches and pixels.
  3. * builds square pixels (emitted as calc()).
  4. % returns the remainder after conversion.

Example 2 — Unitless Mixing

A unitless number can combine with any unit.

SCSS
@use "sass:meta";

.unitless {
  --a: #{meta.inspect(100px + 50)};
  --b: #{meta.inspect(4s * 10)};
}

How It Works

  1. 50 has no unit.
  2. + 100px keeps px.
  3. * 10 scales the seconds.
  4. Matches the official unitless samples.

Example 3 — Divide with math.div

Prefer the module API over deprecated / division.

SCSS
@use "sass:math";
@use "sass:meta";

.div {
  --a: #{meta.inspect(math.div(15px, 30px))};
  --b: #{meta.inspect(math.div(5px, 2s))};
}

How It Works

  1. Same units cancel to a unitless ratio.
  2. px / s becomes a complex unit.
  3. calc() represents that unit in CSS.
  4. Dart Sass 1.33+ for math.div.

Example 4 — Unary + and -

Negate (or leave unchanged) a whole expression.

SCSS
@use "sass:meta";

.unary {
  --a: #{meta.inspect(-(50px + 30px))};
  --b: #{meta.inspect(-(10px - 15px))};
}

How It Works

  1. Parentheses group the inner sum/difference.
  2. Unary - flips the sign.
  3. -(-5px) style math yields 5px.
  4. Wrap unary negation in lists: (-$n).

Example 5 — Practical Spacing Scale

Build padding and margin from a base size with operators and math.div.

SCSS
@use "sass:math";

$base: 16px;
$space: $base * 1.5;

.card {
  padding: $space;
  margin-top: $space - 4px;
  width: math.div(100%, 3);
}

How It Works

  1. * scales the base to 24px.
  2. - trims margin by 4px.
  3. math.div splits 100% into thirds.
  4. Unit-clean variables stay math-ready.

Edge Cases & Pitfalls

Most numeric bugs are units, slash ambiguity, or hyphen spacing.

/

Prefer math.div over /

Literal 15px / 30px may stay slash-separated for CSS. See math.div().

-

Space both sides when subtracting

Write 5px - 3px. For negation in a list, wrap: (-$n).

#{}

Do not build units with interpolation

#{$n}px is a string. Use $n * 1px or unit-bearing variables. See interpolation.

%

Percentages are not decimals

50% ≠ 0.5. Convert with math.div($p, 100%) or $d * 100%.

Sass Support

Numeric operators are core SassScript; division prefers the math module.

APIStatus
+ - * %Supported with unit conversion
math.div()Dart Sass 1.33+ via @use "sass:math"
/ as divisionDeprecated — migrate to math.div
Complex unitsOften emitted as calc() in modern Dart Sass

Compile with the Dart Sass CLI.

Key Takeaways

  • + - * % with automatic compatible-unit conversion.
  • math.div() for division—not slash.
  • Unitless mixes; incompatible + / - / % errors.
  • Keep math unit-clean—no #{$n}px.

One line: do number math with + - * % and math.div(), keeping real units on your values.

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:math" when dividing
2. Use + - * % for everyday arithmetic
3. Space both sides of - when subtracting
4. Keep units on variables (or * 1px)
5. Expect calc() for some complex units

Frequently Asked Questions

They are the standard math operators for numbers: + (add), - (subtract), * (multiply), and % (modulo). They convert between compatible units at compile time.
Use math.div($a, $b) from the sass:math module (Dart Sass 1.33+). Using / as a division operator is deprecated because CSS also uses / as a separator.
Yes. Official docs: unitless numbers can be used with numbers of any unit. Example: 100px + 50 equals 150px.
Addition, subtraction, and modulo with incompatible units (like 100px + 10s) cause a compile error. Multiplication and math.div can produce complex units instead.
No. In Sass, 50% is a number with a % unit, not the same as 0.5. Convert with math.div($percentage, 100%) or $decimal * 100% (or math.percentage()).
Interpolation creates an unquoted string that looks like a number but cannot be used in math. Prefer keeping the unit on the number, or write $number * 1px.

Next: Sass String Operators

Concatenate and work with quoted / unquoted strings.

String operators →

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