Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SASS: calc() vs normal calculation?

Tags:

css

sass

css-calc

I previously started working with SASS (scss to be specific) and was wondering, if there's any difference/advantage/disadvantage between the css calc() method or sass calculation?

$divide-by: 50;

//CSS calc()
.example1 {
  height: calc(100vw / #{$divide-by});
}

//SASS calculation
.example2 {
  height: 100vw / $divide-by;
}
like image 687
Tanckom Avatar asked Oct 18 '18 07:10

Tanckom


People also ask

What is the difference between SCSS and Sass?

SASS (Syntactically Awesome Style Sheets) is a pre-processor scripting language that will be compiled or interpreted into CSS. SassScript is itself a scripting language whereas SCSS is the main syntax for the SASS which builds on top of the existing CSS syntax.

What does Calc method do?

The calc() function performs a calculation to be used as the property value.

Can you use variables in Calc CSS?

On top of using CSS variables in calc you can also convert a value that has no units to a value with units by just multiplying the value by 1 of the unit type you want to convert to. This is very useful if you have CSS variables being set in JS that do not include the unit.

Can you do math in Sass?

SASS allows for mathematical operations such as addition, subtraction, multiplication and division.


1 Answers

SASS calculation is made on compilation. While CSS calc() is always made on the browser. For example:

SASS height: 100px / 2; will compile to height: 50px; <- and this is what the browser will see always no matter what.

CSS width: calc(100% - 20px) will always be this even on the browser, and at that point the browser will do the calculations depending on what that 100% looks like.

in your case height: 100vw / $divide-by; i belive you variable will be considered as a vw value, so if $divide-by is 20, then the compiles version of that height will be height: 5vw;

Let me know if the explanation did not help, and I will try to give you some resources to read.

like image 126
Constantin Chirila Avatar answered Oct 22 '22 04:10

Constantin Chirila