Home Sass Numbers Sass Numbers Definition
Definition and Usage A Sass number has two parts: the numeric value and optional units (for example 16px). Numbers can also be unitless.
Real-world unit math. Multiply and divide units the way physics does—px * px is square pixels; math.div(5px, 2s) is pixels per second.
Stay unit-clean. Prefer $n * 1px over #{$n}px. Interpolation makes a string , not a number.
50% ≠ 0.5. Percentages are a unit. Convert with math.div($pct, 100%) or $dec * 100%.
Syntax
Syntax @use "sass:math";
100; // unitless
16px; // value + unit
5.2e3; // scientific → 5200
4px * 6px; // complex units → calc(...)
math.div(5px, 2s); // px / s
1in + 6px; // compatible → converts
math.div(50%, 100%); // 0.5
0.5 * 100%; // 50%
$size * 1px; // attach a unit safely Pattern: write a number (with units if needed) → do math with matching / compatible units → use math.div for division.
Code
SCSS Examples Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).
1. Basics 2. Units 3. Speed 4. Percent 5. Clean Example 1 — Official Number Forms Unitless, decimals, units, and scientific notation.
.demo {
--int: #{100};
--dec: #{0.8};
--px: #{16px};
--sci: #{5.2e3};
--sci2: #{6e-2};
} .demo {
--int: 100;
--dec: 0.8;
--px: 16px;
--sci: 5200;
--sci2: 0.06;
} How It Works 100 / 0.8 are unitless numbers.16px keeps its unit.Scientific form expands in CSS.No int vs float split—like JavaScript.Example 2 — Official Unit Math Multiply/divide units; convert compatible lengths.
@use "sass:math";
.demo {
--sq: #{4px * 6px};
--rate: #{math.div(5px, 2s)};
--sum: #{1in + 6px};
} .demo {
--sq: calc(24px * 1px);
--rate: calc(2.5px / 1s);
--sum: 1.0625in;
} How It Works px * px becomes complex units in calc().math.div builds a rate (px / s).1in + 6px converts (CSS: 1in = 96px).1in + 1s would error (incompatible).Example 3 — Official Unit Cancellation A seconds-per-pixel ratio times a distance cancels to time.
@use "sass:math";
$transition-speed: math.div(1s, 50px);
@mixin move($left-start, $left-stop) {
position: absolute;
left: $left-start;
transition: left ($left-stop - $left-start) * $transition-speed;
&:hover {
left: $left-stop;
}
}
.slider {
@include move(10px, 120px);
} .slider {
position: absolute;
left: 10px;
transition: left 2.2s;
}
.slider:hover {
left: 120px;
} How It Works 1s / 50px is the speed ratio.110px of travel × ratio → 2.2s.px cancels ; seconds remain.Matches the official Sass docs sample.Example 4 — Percentages vs Decimals 50% is not equal to 0.5—convert explicitly.
@use "sass:math";
$pct: 50%;
$dec: 0.5;
.demo {
--same: #{$pct == $dec};
--to-dec: #{math.div($pct, 100%)};
--to-pct: #{$dec * 100%};
--via-fn: #{math.percentage($dec)};
} .demo {
--same: false;
--to-dec: 0.5;
--to-pct: 50%;
--via-fn: 50%;
} How It Works 50% == 0.5 is false.math.div(..., 100%) yields a decimal.* 100% or math.percentage() yields a percent.Keep units meaningful for CSS.Example 5 — Unit-Clean Attachment Multiply by 1px instead of interpolating a fake number.
@use "sass:math";
$size: 16;
.box {
width: $size * 1px;
height: $size * 1px;
margin: math.div(24px, 2);
} .box {
width: 16px;
height: 16px;
margin: 12px;
} How It Works $size is unitless.* 1px creates a real length number.math.div keeps units on the result.Avoid #{$size}px (that is a string). Edge Cases & Pitfalls Most number bugs are stringified units or mixing incompatible units.
#{} Do not build units with interpolation #{$n}px is a string. Use $n * 1px or store the unit on the variable. See interpolation .
% Percentages are not decimals Convert explicitly with unit arithmetic or math.percentage() .
units Incompatible units error early That is usually a real math bug—fix the units rather than stripping them.
Toolchain
Sass Support Numbers and unit math are core Sass on Dart Sass.
Feature Status Numbers with units Supported Complex units as calc() Dart Sass (CSS emission; fuller support since 1.96) ~10 digit CSS precision Dart Sass default sass:math helpersSupported
Compile with the Dart Sass CLI .
Remember
Key Takeaways Value + units make a Sass number.
Unit math multiplies, divides, and cancels like the real world.
50% ≠ 0.5 —convert with unit arithmetic.
$n * 1px , never #{$n}px.
One line: keep numbers unit-clean so Sass can convert, cancel, and catch bad math for you.
Practice
Compile Notes Practical checklist:
1. Give quantities the units they mean
2. Use math.div for division
3. Convert % ↔ decimals explicitly
4. Attach units with $n * 1px (not #{$n}px)
5. Let incompatible-unit errors guide your math Frequently Asked Questions What is a Sass number? A Sass number has two parts: the numeric value and optional units. For example, in 16px the value is 16 and the unit is px. Numbers can also be unitless.
Are percentages the same as decimals in Sass? No. 50% is a number with a % unit and is not the same as 0.5. Convert with math.div($percentage, 100%) or $decimal * 100% (or math.percentage()).
Why is #{$number}px a bad habit? Interpolation creates an unquoted string that looks like a number but cannot be used with number math or functions. Prefer $number * 1px or keep the unit on the variable.
What happens with incompatible units? Sass converts compatible units automatically (for example 1in + 6px). Combining incompatible units such as 1in + 1s throws a compile error.
How precise are Sass numbers? Sass uses 64-bit floats internally and serializes with up to about 10 digits after the decimal for CSS output and equality checks.
Does Sass treat integers differently from decimals? No. Sass does not distinguish whole numbers from decimals. math.div(5, 2) returns 2.5—similar to JavaScript.
Next: Sass Strings Learn quoted vs unquoted strings and when interpolation helps.
Strings → 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