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:
p {
font-size: 5px + 10px;
}
Compilation result:
p {
font-size: 15px;
}
Example
Let's subtract one value from another:
p {
font-size: 25px - 10px;
}
Compilation result:
p {
font-size: 15px;
}
Example
Let's multiply the value by a number:
p {
font-size: 5px * 5;
}
Compilation result:
p {
font-size: 25px;
}
Example
Let's divide the value by the number:
div {
width: 100% / 3;
}
Compilation result:
div {
width: 33.33333333%;
}
Example
Let's add a value to the contents of the variable:
@var: 100px;
div {
width: @var + 100px;
}
Compilation result:
div {
width: 200px;
}
Example
Let's add two variables:
@var1: 100px;
@var2: 200px;
div {
width: @var1 + @var2;
}
Compilation result:
div {
width: 300px;
}
Example
When attempting an operation with different units, LESS converts all units to one - the first one. Example:
div {
width: 100% - 10px;
}
Compilation result:
div {
width: 90%;
}
Practical tasks
Tell me what the result of compiling the following code will be:
#block {
width: 100px * 2;
}
Tell me what the result of compiling the following code will be:
#block {
width: 200px / 3;
}
Tell me what the result of compiling the following code will be:
@var1: 500px;
@var2: 5;
#block {
width: @var1 / @var2;
}