Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control Sass Variable with javascript

I have a Sass file which is generating a CSS file. I have used many variables in my sass file for background color, font-size, now I want to control my all variables through JavaScript.

For example, in style.sass we have

$bg : #000; $font-size: 12px; 

How can I change these values from JavaScript?

like image 475
JA9 Avatar asked Jul 22 '13 12:07

JA9


People also ask

Can I use SASS variables in JS?

To make Sass (or, specifically, SCSS in this case) variables available to JavaScript, we need to “export” them. The :export block is the magic sauce webpack uses to import the variables. What is nice about this approach is that we can rename the variables using camelCase syntax and choose what we expose.

Can you change SCSS variable value dynamically?

SCSS is compiled to CSS during compile time, and SCSS variables are replaced with resolved value during compile time, which means there is no way to change the variable during run time. However, CSS variables just sits there during run time, and you can dynamically CRUD them during run time with JavaScript (Web API).

How do you change a variable value in SASS?

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.

Can JavaScript update CSS variable?

CSS variables have access to the DOM, which means that you can change them with JavaScript.


1 Answers

You can't. SASS is a CSS preprocessor, which means that all SASS specific information disapears when you compile it to CSS.

like image 50
GJK Avatar answered Oct 07 '22 13:10

GJK