Mathematical Operations in SASS

In SASS language, you can perform mathematical operations on variables and different values. Let's look at examples.

Example

Let's add the two values:

styles.scssp {
  font-size: 5px + 10px;
}

Compilation result:

styles.cssp {
  font-size: 15px;
}

Example

Let's subtract one value from another:

styles.scssp {
  font-size: 25px - 10px;
}

Compilation result:

styles.cssp {
  font-size: 15px;
}

Example

Let's multiply the value by a number:

styles.scssp {
  font-size: 5px * 5;
}

Compilation result:

styles.cssp {
  font-size: 25px;
}

Example

Let's divide the value by the number:

styles.scssdiv {
  width: 100% / 3;
}

Compilation result:

styles.cssdiv {
  width: 33.33333333%;
}

Example

Let's add a value to the contents of the variable:

styles.scss$var: 100px;

div {
  width: $var + 100px;
}

Compilation result:

styles.cssdiv {
  width: 200px;
}

Example

Let's add two variables:

styles.scss$var1: 100px;
$var2: 200px;

div {
  width: $var1 + $var2;
}

Compilation result:

styles.cssdiv {
  width: 300px;
}

Example

When attempting an operation with different units, SCSS converts all units to one - the first one. Example:

styles.scssdiv {
  width: 100% - 10px;
}

Compilation result:

styles.cssdiv {
  width: 90%;
}

Practical tasks

Задание 1

Tell me what the result of compiling the following code will be:

styles.scss#block {
  width: 100px * 2;
}
Задание 2

Tell me what the result of compiling the following code will be:

styles.scss#block {
  width: 200px / 3;
}
Задание 3

Tell me what the result of compiling the following code will be:

styles.scss$var1: 500px;
$var2: 5;

#block {
  width: $var1 / $var2;
}