Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 39 ยท Variables, Nesting and Partials

Sass Data Types and Operators

Learn the seven Sass data types, arithmetic with units, comparison and logical operators, string concatenation and why you use math.div instead of the slash.

Sass values are not just text. Every value has a type, and the type decides what you are allowed to do with it. You can add two numbers, but not a number and a color. Knowing the types makes error messages easier to read and unlocks the real power of the language: computing values rather than typing them.

The Sass data types

Type Examples Notes
Number 12, 4.5, 16px, 50%, 1.5rem Numbers carry a unit, or none
String "Inter", sans-serif, 'a b' Quoted or unquoted
Color #0ea5e9, rgb(14 165 233), hsl(199 89% 48%), red Real color values, not text
Boolean true, false Result of comparisons
Null null "Nothing". Declarations with a null value are dropped from the output
List 10px 20px, a, b, c, [x y] Space, comma or bracket separated
Map (sm: 576px, md: 768px) Key/value pairs (Maps)

There are also function and mixin references (first-class values you can pass around) and a few special types, but those seven cover everyday work. The meta.type-of() function, from the built-in sass:meta module, tells you which type a value has.

Ask Sass for the type of a value

Numbers and units

A Sass number is a value plus an optional unit. Sass keeps the unit and does the math with it:

SCSS
.box {
  width: 10px + 5px;     // 15px
  height: 2 * 8px;       // 16px
  margin: 10px - 4px;    // 6px
  padding: 7 % 3 * 1px;  // 1px
}

If the units are compatible (px, in, cm, mm and the like), Sass converts them. If they are not (px and em, or px and %), it raises an error instead of guessing: 10px + 1em is rejected because the browser, not Sass, knows how big an em is. For mixed units use CSS calc().

SCSS
width: calc(100% - 2rem);   // plain CSS, passed through

Multiplying two lengths gives a strange unit (px*px), which is invalid CSS. Multiply a length by a unitless number instead.

Division: use math.div

In CSS the slash separates values (font: 16px/1.5 serif, grid-area: 1 / 3). Sass used to treat / as division in some positions, which caused confusion, so it is deprecated as a division operator. Use the sass:math module:

SCSS
@use "sass:math";

.col {
  width: math.div(100%, 3);        // 33.3333333333%
  font-size: math.div(24px, 16px) * 1rem;   // 1.5rem
}

Notice the second line: dividing 24px by 16px cancels the units and leaves the plain number 1.5, which you can then multiply by 1rem. This "px to rem" trick appears in nearly every design system, and Custom functions turns it into a reusable rem() function.

sass:math also offers math.round, math.ceil, math.floor, math.abs, math.min, math.max, math.pow, math.sqrt, math.percentage and math.clamp; Built-in math, string and list functions walks through them.

Strings

Strings may be quoted ("Inter") or unquoted (sans-serif). The difference is mostly about output: quoted strings print with quotes, and unquoted strings print as written. You rarely care, but two rules help:

  • + joins strings: "icon-" + "home" gives "icon-home". The left side decides whether the result is quoted.
  • To insert a value anywhere, including selectors and property names, use interpolation: .icon-#{$name}.

Comparison and logical operators

Operators Meaning
==, != Equal, not equal (work on every type)
<, <=, >, >= Ordering (numbers only, compatible units)
and, or, not Boolean logic
SCSS
$width: 800px;
$wide: $width >= 768px;                    // true
$tablet: $width >= 600px and $width < 768px;   // false
$odd: not ($width == 800px);               // false

Sass has a short list of "falsy" values: only false and null are false. Zero, an empty string and an empty list are all truthy, which differs from JavaScript. This matters in conditions.

Sass also lets you compare types sensibly: 1px == 1px is true, 1 == 1px is false (different units), and "a" == a is true (quotes do not matter).

Operators in practice

Run it and notice the .zero rule: 0 counts as true in Sass.

Colors

Sass understands colors as values, so you can compute with them. Arithmetic on colors is no longer supported (#fff - #111 is an error); use the sass:color functions instead. Built-in color functions covers color.adjust, color.scale, color.mix and friends.

Lists and maps at a glance

A list is an ordered series: $sizes: 8px 16px 24px; or $fonts: "Inter", Arial, sans-serif;. CSS properties such as margin and font-family already take lists. A map pairs keys with values, perfect for design tokens. Both get their own treatment in Maps and in the list functions lesson, because they power loops.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens with 10px + 1em in Sass?

    px and em cannot be converted at compile time, so Sass stops with an error. Write calc(10px + 1em) yourself if you want the browser to do it.
  2. 2. Which is the recommended way to divide in modern Sass?

    The slash is deprecated as a division operator. Load sass:math with @use and call math.div().
  3. 3. Which values are falsy in Sass?

    In Sass, only false and null are falsy. Zero and empty strings are truthy, unlike in JavaScript.
  4. 4. What does meta.type-of((a: 1)) return?

    Parenthesized key/value pairs form a map. (Note: an empty pair of parentheses is both an empty list and an empty map.)

Summary

  • Sass has numbers, strings, colors, booleans, null, lists and maps; meta.type-of() reveals the type.
  • Numbers keep their units; Sass converts compatible units and errors on incompatible ones.
  • Divide with math.div(), because the / operator is deprecated for division.
  • + concatenates strings; and, or, not combine booleans; only false and null are falsy.
  • Color math uses the sass:color functions, not operators.

Next: Nesting