Numeric Operators

Sass supports the standard set of mathematical operators for numbers. They automatically convert between compatible units.

  • <expression> + <expression> adds the first expression’s value to the second’s.
  • <expression> - <expression> subtracts the first expression’s value from the second’s.
  • <expression> * <expression> multiplies the first expression’s value by the second’s.
  • <expression> / <expression> divides the first expression’s value by the second’s.
  • <expression> % <expression> returns the remainder of the first expression’s value divided by the second’s. This is known as the modulo operator.

SCSS Syntax

@debug 10s + 15s; // 25s
@debug 1in - 10px; // 0.8958333333in
@debug 5px * 3px; // 15px*px
@debug (12px/4px); // 3
@debug 1in % 9px; // 0.0625in

Sass Syntax

@debug 10s + 15s  // 25s
@debug 1in - 10px  // 0.8958333333in
@debug 5px * 3px  // 15px*px
@debug (12px/4px)  // 3
@debug 1in % 9px  // 0.0625in

Unitless numbers can be used with numbers of any unit.

SCSS Syntax

@debug 100px + 50; // 150px
@debug 4s * 10; // 40s

Sass Syntax

@debug 100px + 50  // 150px
@debug 4s * 10  // 40s

Numbers with incompatible units can’t be used with addition, subtraction, or modulo.

SCSS Syntax

@debug 100px + 10s;
//     ^^^^^^^^^^^
// Error: Incompatible units px and s.

Sass Syntax

@debug 100px + 10s
//     ^^^^^^^^^^^
// Error: Incompatible units px and s.

Unary Operators

You can also write + and - as unary operators, which take only one value:

  • +<expression> returns the expression’s value without changing it.
  • -<expression> returns the negative version of the expression’s value.

SCSS Syntax

@debug +(5s + 7s); // 12s
@debug -(50px + 30px); // -80px
@debug -(10px - 15px); // 5px

Sass Syntax

@debug +(5s + 7s)  // 12s
@debug -(50px + 30px)  // -80px
@debug -(10px - 15px)  // 5px

Slash-Separated Values

A few CSS properties support / as a way of separating values. This means Sass has to disambiguate between / as a property value and / as division. In order to make this work, if two numbers are separated by /, Sass will print the result as slash-separated instead of divided unless one of these conditions is met:

  • Either expression is anything other than a literal number.
  • The result is stored in a variable or returned by a function.
  • The operation is surrounded by parentheses, unless those parentheses are outside a list that contains the operation.
  • The result is used as part of another operation (other than /).

If you want to force / to be used as a separator, you can write it as #{<expression>} / #{<expression>}.

SCSS Syntax

@debug 15px / 30px; // 15px/30px
@debug (10px + 5px) / 30px; // 0.5
@debug #{10px + 5px} / 30px; // 15px/30px

$result: 15px / 30px;
@debug $result; // 0.5

@function fifteen-divided-by-thirty() {
  @return 15px / 30px;
}
@debug fifteen-divided-by-thirty(); // 0.5

@debug (15px/30px); // 0.5
@debug (bold 15px/30px sans-serif); // bold 15px/30px sans-serif
@debug 15px/30px + 1; // 1.5

Sass Syntax

@debug 15px / 30px  // 15px/30px
@debug (10px + 5px) / 30px  // 0.5

$result: 15px / 30px
@debug $result  // 0.5

@function fifteen-divided-by-thirty()
  @return 15px / 30px

@debug fifteen-divided-by-thirty()  // 0.5

@debug (15px/30px)  // 0.5
@debug (bold 15px/30px sans-serif)  // bold 15px/30px sans-serif
@debug 15px/30px + 1  // 1.5

Units

Sass has powerful support for manipulating units based on how real-world unit calculations work. When two numbers are multiplied, their units are multiplied as well. When one number is divided by another, the result takes its numerator units from the first number and its denominator units from the second. A number can have any number of units in the numerator and/or denominator.

SCSS Syntax

@debug 4px * 6px; // 24px*px (read "square pixels")
@debug 5px / 2s; // 2.5px/s (read "pixels per second")
@debug 5px * 30deg / 2s / 24em; // 3.125px*deg/s*em
                                // (read "pixel-degrees per second-em")

$degrees-per-second: 20deg/1s;
@debug $degrees-per-second; // 20deg/s
@debug 1 / $degrees-per-second; // 0.05s/deg

Sass Syntax

@debug 4px * 6px  // 24px*px (read "square pixels")
@debug 5px / 2s  // 2.5px/s (read "pixels per second")
@debug 5px * 30deg / 2s / 24em  // 3.125px*deg/s*em
//                                 (read "pixel-degrees per second-em")

$degrees-per-second: 20deg/1s
@debug $degrees-per-second  // 20deg/s
@debug 1 / $degrees-per-second  // 0.05s/deg

Sass will automatically convert between compatible units, although which unit it will choose for the result depends on which implementation of Sass you’re using.If you try to combine incompatible units, like 1in + 1em, Sass will throw an error.

SCSS Syntax

// CSS defines one inch as 96 pixels.
@debug 1in + 6px; // 102px or 1.0625in

@debug 1in + 1s;
//     ^^^^^^^^
// Error: Incompatible units s and in.

Sass Syntax

// CSS defines one inch as 96 pixels.
@debug 1in + 6px  // 102px or 1.0625in

@debug 1in + 1s
//     ^^^^^^^^
// Error: Incompatible units s and in.

As in real-world unit calculations, if the numerator contains units that are compatible with units in the denominator (like 96px / 1in), they’ll cancel out. This makes it easy to define a ratio that you can use for converting between units. In the example below, we set the desired speed to one second per 50 pixels, and then multiply that by the number of pixels the transition covers to get the time it should take.

SCSS Syntax

$transition-speed: 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);
}

Sass Syntax

$transition-speed: 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)

CSS Output

.slider {
  position: absolute;
  left: 10px;
  transition: left 2.2s;
}
.slider:hover {
  left: 120px;
}