Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to set SCSS variable to CSS variable?

Tags:

css

sass

Consider the following SCSS:

$color-black: #000000;  body {     --color: $color-black; } 

When it is compiled with node-sass version 4.7.2, it produces following CSS:

body {     --color: #000000;  } 

When I compile the same SCSS with version 4.8.3 or higher, it produces following:

body {     --color: $color-black;  } 

What am I missing? I checked release logs, but could not found anything useful. Also, I wonder if this change is genuine why does it have only minor version change? Should it not be a major release?

Also, what is my alternative? Should I use Interpolation?

like image 857
Harshal Patil Avatar asked May 06 '18 18:05

Harshal Patil


People also ask

How do I assign a Sass variable to a variable in CSS?

To convert the SASS variable to a CSS custom property you put curly brackets around it, and a hash in front. If you've used template literals in JavaScript it's the same thing, just with a # instead of a $ (because we already have $ in the variable name).

Does SCSS support CSS variables?

We are all gladly using SCSS and benefiting from its features. In addition to that, we didn't want to give up on CSS variables, because they are strong, especially when it comes to changing them in run-time, it can't be achieved using SCSS, SASS, or LESS variables.

How do I change variables in SCSS?

A Sass variable must be initialized with a value. To change the value, we simply replace the old value with a new one. A variable that's initialized inside a selector can only be used within that selector's scope. A variable that's initialized outside a selector can be used anywhere in the document.

Does SCSS compile to CSS?

scss already imports all other scss files of template. So when any SCSS file in the scss folder is updated, it will be compiled to . css files. Now, you can start working with SCSS files.


1 Answers

Just use string interpolation:

$color-black: #000000;  body {     --color: #{$color-black}; } 

Apparently the old behaviour is not intended and violated the language specs of SASS:

  • CSS variables mixed with SCSS variables don't emit proper CSS in 4.8+
  • CSS variables aren't properly compiled
  • Assigning SASS variables to CSS Variables (Custom Properties) no longer works
like image 77
Terry Avatar answered Oct 08 '22 16:10

Terry