TExcelTAD
Home › Sass › Sass Operators & Math

Sass Operators & Math

Sass can calculate values while it compiles. You can add spacing values, multiply a base size, compare numbers and combine conditions — and the sass:math module adds rounding, division, powers and more.

Arithmetic operators

Operator Meaning Example Result
+ Addition 10px + 6px 16px
- Subtraction 1rem - 0.25rem 0.75rem
* Multiplication 8px * 3 24px
math.div() Division math.div(24px, 2) 12px
% Modulo (remainder) 17 % 5 2
scss · try it yourself
@use "sass:math";

$base: 8px;
$columns: 12;

.stack > * + * {
  margin-top: $base * 2;
}

.col-4 {
  width: math.percentage(math.div(4, $columns));
}

.sidebar {
  padding: $base + 4px $base * 3;
  width: 300px - $base * 2;   // * happens before -
}
.stack > * + * {
  margin-top: 16px;
}

.col-4 {
  width: 33.3333333333%;
}

.sidebar {
  padding: 12px 24px;
  width: 284px;
}

Units

Sass numbers carry units and follow real math rules:

  • 10px + 5px → 15px; 1in + 6px → Sass converts compatible units (1.0625in).
  • 10px * 2 → 20px, but 10px * 2px would be 20px*px, which isn't valid CSS and causes an error when output.
  • Dividing two values with the same unit removes it: math.div(32px, 16px) → 2.
  • Incompatible units like px + em can't be added at compile time → error. Use CSS calc() instead.

A classic use: converting pixels to rem.

scss · try it yourself
@use "sass:math";

@function rem($px, $base: 16px) {
  @return math.div($px, $base) * 1rem;
}

h1 { font-size: rem(32px); }
small { font-size: rem(12px); }
.mixed { width: calc(100% - #{rem(24px)}); }
h1 {
  font-size: 2rem;
}

small {
  font-size: 0.75rem;
}

.mixed {
  width: calc(100% - 1.5rem);
}

Why not / for division?

In CSS, / is a separator: font: 16px/1.5 serif, grid-area: 1 / 3. Older Sass also used / for division, which was ambiguous. Division with / is deprecated; use math.div() (or calc()). Sass shows a warning when you divide with /:

scss · try it yourself
$gutter: 30px;

.box {
  padding: $gutter / 2;
}
.box {
  padding: 15px;
}

Deprecation warning (line 4): Using / for division outside of calc() is deprecated and will be removed in Dart Sass 2.0.0.

The sass:math module

scss · try it yourself
@use "sass:math";

.demo {
  a: math.round(4.6px);
  b: math.ceil(4.1px);
  c: math.floor(4.9px);
  d: math.abs(-12px);
  e: math.max(3px, 9px, 5px);
  f: math.pow(2, 10);
  g: math.sqrt(81);
  h: math.percentage(0.25);
  i: math.clamp(10px, 4px, 20px);
  j: math.unit(16px);
}
.demo {
  a: 5px;
  b: 5px;
  c: 4px;
  d: 12px;
  e: 9px;
  f: 1024;
  g: 9;
  h: 25%;
  i: 10px;
  j: "px";
}

Comparison and boolean operators

These are mostly used inside @if and @while:

Operator Meaning
==, != Equal / not equal (works on any value)
<, <=, >, >= Numeric comparison
and, or, not Boolean logic (words, not && / `
scss · try it yourself
$width: 900px;
$dark: false;

.info {
  wide: $width > 768px;
  same: 1in == 96px;
  logic: $width > 600px and not $dark;
}
.info {
  wide: true;
  same: true;
  logic: true;
}

Tip: 1in == 96px is true because Sass converts compatible units before comparing.

String concatenation with +

+ also joins strings: "icon-" + home → "icon-home". If the first string is quoted, the result is quoted. For building selectors and property names, interpolation (#{}) is usually clearer — see the next lesson.

Test yourself

What is the modern way to divide 24px by 2 in Sass?

  1. 24px / 2
  2. math.div(24px, 2)
  3. div(24px, 2)
  4. 24px % 2

What does math.div(32px, 16px) return?

  1. 2px
  2. 2
  3. 2rem
  4. An error

Which operator is NOT valid boolean logic in Sass?

  1. and
  2. or
  3. not
  4. &&