Sub topic 7
Arithmetic and sass:math
This chapter assumes you can write Sass variables and compile a file with the Dart Sass command line. It covers the arithmetic operators, unit handling, and the sass:math module that replaced the / operator for division.
Operators and units
Sass can calculate values at compile time. The operators +, -, * and % work on numbers, and the result is written into the CSS as a plain value.
$gutter: 16px;
.grid {
padding: $gutter * 2;
margin-left: -$gutter;
width: calc(100% - #{$gutter * 2});
}This compiles to:
.grid {
padding: 32px;
margin-left: -16px;
width: calc(100% - 32px);
}Sass tracks units during calculation, which gives these rules:
- Numbers with the same unit combine:
10px + 5pxis15px. - Multiplying by a unitless number keeps the unit:
8px * 3is24px. - Compatible units convert to the left-hand unit:
1in + 96pxis2in. - Incompatible units fail at compile time:
10px + 1emraises "Incompatible units em and px." - Multiplying two lengths gives an invalid unit:
10px * 2pxis20px*px, and Sass reports an error if that reaches the CSS.
Loading sass:math
Built-in modules are loaded with @use. By default, functions are available through the module name as a namespace.
@use 'sass:math';
.col {
width: math.percentage(math.div(1, 3));
font-size: math.div(24px, 16px) * 1rem;
}math.div(1, 3) returns 0.3333333333. Sass keeps ten digits of precision. math.percentage() turns that into 33.3333333333%. math.div(24px, 16px) cancels the units and returns the unitless number 1.5. Multiplying by 1rem then attaches the new unit, giving 1.5rem.
Common math functions
| Function | Example | Result |
|---|---|---|
| `math.div($a, $b)` | `math.div(10, 4)` | `2.5` |
| `math.round($n)` | `math.round(2.5)` | `3` |
| `math.ceil($n)` | `math.ceil(2.1)` | `3` |
| `math.floor($n)` | `math.floor(2.9)` | `2` |
| `math.abs($n)` | `math.abs(-4px)` | `4px` |
| `math.min($n...)` | `math.min(4px, 2px)` | `2px` |
| `math.max($n...)` | `math.max(4px, 2px)` | `4px` |
| `math.pow($b, $e)` | `math.pow(2, 3)` | `8` |
| `math.sqrt($n)` | `math.sqrt(16)` | `4` |
The module also provides constants such as math.$pi. Variables from a module need the $ after the namespace, as in math.$pi.
A worked example: a modular type scale
Combine variables, math.pow() and math.div() to compute a type scale from one ratio.
@use 'sass:math';
$base: 1rem;
$ratio: 1.25;
@function step($n) {
@return math.div(math.round(math.pow($ratio, $n) * $base * 100), 100);
}
h1 { font-size: step(3); }
h2 { font-size: step(2); }
p { font-size: step(0); }math.pow(1.25, 3) is 1.953125. Multiplying by 1rem * 100 gives 195.3125rem. Rounding gives 195rem, and dividing by 100 gives 1.95rem. The result is a value rounded to two decimal places, and the unit stays attached throughout.
Common errors
- Using
/for division. You get a deprecation warning, or the slash is passed through to the CSS as a separator. - Forgetting the namespace. Writing
div(1, 3)fails because@use 'sass:math'requiresmath.div. - Mixing incompatible units. Use
calc()with interpolation when the browser must combine them, as incalc(100% - #{$gutter}). - Expecting unit removal.
math.div(24px, 16px)is unitless, so you must multiply by1remto get a length back.
Exercise
What does math.div(45, 15) * 1em compile to, and why does math.div(45px, 15px) * 1em produce the same result?
Show answer
Both compile to 3em. math.div(45, 15) is 3. In the second case the px units cancel, so the quotient is also the unitless 3. Multiplying by 1em then attaches the em unit.