Home Sass Operators Numeric Sass Numeric Operators Definition
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
Syntax @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.
Code
SCSS Examples Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).
1. Official 2. Unitless 3. math.div 4. Unary 5. Spacing Example 1 — Official + - * % Add times, subtract convertible lengths, multiply units, and take modulo.
@use "sass:meta";
.probe {
--a: #{meta.inspect(10s + 15s)};
--b: #{meta.inspect(1in - 10px)};
--c: #{meta.inspect(5px * 3px)};
--d: #{meta.inspect(1in % 9px)};
} .probe {
--a: 25s;
--b: 0.8958333333333334in;
--c: calc(15px * 1px);
--d: 0.0625in;
} How It Works + adds matching time units.- converts inches and pixels.* builds square pixels (emitted as calc()).% returns the remainder after conversion.Example 2 — Unitless Mixing A unitless number can combine with any unit.
@use "sass:meta";
.unitless {
--a: #{meta.inspect(100px + 50)};
--b: #{meta.inspect(4s * 10)};
} .unitless {
--a: 150px;
--b: 40s;
} How It Works 50 has no unit.+ 100px keeps px.* 10 scales the seconds.Matches the official unitless samples.Example 3 — Divide with math.div Prefer the module API over deprecated / division.
@use "sass:math";
@use "sass:meta";
.div {
--a: #{meta.inspect(math.div(15px, 30px))};
--b: #{meta.inspect(math.div(5px, 2s))};
} .div {
--a: 0.5;
--b: calc(2.5px / 1s);
} How It Works Same units cancel to a unitless ratio.px / s becomes a complex unit.calc() represents that unit in CSS.Dart Sass 1.33+ for math.div.Example 4 — Unary + and - Negate (or leave unchanged) a whole expression.
@use "sass:meta";
.unary {
--a: #{meta.inspect(-(50px + 30px))};
--b: #{meta.inspect(-(10px - 15px))};
} .unary {
--a: -80px;
--b: 5px;
} How It Works Parentheses group the inner sum/difference.Unary - flips the sign.-(-5px) style math yields 5px.Wrap unary negation in lists: (-$n).Example 5 — Practical Spacing Scale Build padding and margin from a base size with operators and math.div.
@use "sass:math";
$base: 16px;
$space: $base * 1.5;
.card {
padding: $space;
margin-top: $space - 4px;
width: math.div(100%, 3);
} .card {
padding: 24px;
margin-top: 20px;
width: 33.3333333333%;
} How It Works * scales the base to 24px.- trims margin by 4px.math.div splits 100% into thirds.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%.
Toolchain
Sass Support Numeric operators are core SassScript; division prefers the math module.
API Status + - * %Supported with unit conversion math.div()Dart Sass 1.33+ via @use "sass:math" / as divisionDeprecated — migrate to math.div Complex units Often emitted as calc() in modern Dart Sass
Compile with the Dart Sass CLI .
Remember
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.
Practice
Compile Notes Practical checklist:
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 What are Sass numeric operators? They are the standard math operators for numbers: + (add), - (subtract), * (multiply), and % (modulo). They convert between compatible units at compile time.
How do I divide numbers in Sass? 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.
Can unitless numbers mix with units? Yes. Official docs: unitless numbers can be used with numbers of any unit. Example: 100px + 50 equals 150px.
What happens with incompatible units? Addition, subtraction, and modulo with incompatible units (like 100px + 10s) cause a compile error. Multiplication and math.div can produce complex units instead.
Are percentages the same as decimals? 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()).
Why avoid #{$number}px? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion