Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using css variables in sass functions - node-sass

I am trying to use CSS variables in an Angular project together with Sass functions (lighten() / darken() to be precise), and as far as I know, the latest version of LibSass allows it.

I have installed [email protected] and [email protected] (and on Angular v7.3.0), but yet I receive an error

Argument $color of lighten($color, $amount) must be a color

Am I missing something?
Thanks!

like image 402
Dan R. Avatar asked Apr 24 '19 06:04

Dan R.


People also ask

Can you use CSS variables with Sass?

Sass variables are all compiled away by Sass. CSS variables are included in the CSS output. CSS variables can have different values for different elements, but Sass variables only have one value at a time.

Can I use CSS custom properties in Sass?

Because of this, Sass parses custom property declarations differently than other property declarations. All tokens, including those that look like SassScript, are passed through to CSS as-is. The only exception is interpolation, which is the only way to inject dynamic values into a custom property.

How do I declare a variable in Sass?

Declaration of a variable in SASS: In SASS, you can define a variable by using $ symbol at the starting of the name of the variable and followed by its value. Understanding scope of a variable: SASS variables can be declared anywhere in the document before it is used.

Is it possible to convert a Sass variable to CSS Variable?

Unfortunately you can't... lighten is a SASS function at compile-time, while CSS variables are run-time variables. You can make a SASS variable into a CSS variable, but not the other way round.

How to call Sass functions from variables?

To conclude, if you need to call SASS functions, just use SASS variables (as arguments to those functions). The expected output is impossible because rgba (color, alpha) is a Sass function. The color argument must be a Sass Color type.

Why does the sass example which used a CSS Variable fail?

The example which used a CSS variable failed. Using SASS’s rgba () function with CSS variables fails to be rendered correctly. According to the official CSS spec, “the values of custom properties are substituted as is when replacing var () references in a property’s value”.

Why should I use Sass for CSS?

Also, if you use Sass, your CSS code will be compatible with all versions of browsers. Sass also makes it possible to reuse your code by creating variables and functions with mixins (cutting up pieces of code) that can be reused over and over again.


1 Answers

Unfortunately you can't... lighten is a SASS function at compile-time, while CSS variables are run-time variables.

You can make a SASS variable into a CSS variable, but not the other way round.

To conclude, if you need to call SASS functions, just use SASS variables (as arguments to those functions).

More explanations from the maintainer of node-sass:
https://github.com/sass/node-sass/issues/2251#issuecomment-372009293

The expected output is impossible because rgba(color, alpha) is a Sass function.
The color argument must be a Sass Color type.
Sass does not evaluation css custom property values, because these are run-time variables.

like image 150
Kelvin Lai Avatar answered Oct 04 '22 13:10

Kelvin Lai