Mathematical Operations in LESS

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

Example

Let's add the two values:

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

Compilation result:

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

Example

Let's subtract one value from another:

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

Compilation result:

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

Example

Let's multiply the value by a number:

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

Compilation result:

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

Example

Let's divide the value by the number:

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

Compilation result:

styles.cssdiv {
  width: 33.33333333%;
}

Example

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

styles.less@var: 100px;

div {
  width: @var + 100px;
}

Compilation result:

styles.cssdiv {
  width: 200px;
}

Example

Let's add two variables:

styles.less@var1: 100px;
@var2: 200px;

div {
  width: @var1 + @var2;
}

Compilation result:

styles.cssdiv {
  width: 300px;
}

Example

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

styles.lessdiv {
  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.less#block {
  width: 100px * 2;
}
Задание 2

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

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

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

styles.less@var1: 500px;
@var2: 5;

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