I have defined a color in my root:
:root { --purple: hsl(266, 35%, 70%); }
And I'm trying to use it in a SASS function to give it transparency:
.purple { background: transparentize(#{"var(--primary-color)"}, 0.7) }
Does anyone know of a way to get this to work? Or is it just not possible?
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.
The basic syntax for defining a variable is simple: Just use a $ before the variable name and treat its definition like a CSS rule: Sass Variable Syntax: $<variable name>:<value>; The following declares a variable named large-font.
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).
We can dynamically update SCSS variables using ReactJS with the help of a project by achieving theme switching of the card component between light and dark theme. Prerequisite: Basic knowledge of npm & create-react-app command.
Global variables can be defined outside of an element in a :root pseudo-class:
:root { --color-background: #FFFFFF; }
you can define a function like this:
@function color($color-name) { @return var(--color-#{$color-name}); }
and call it into your sass:
body { color: color(primary); }
compiled sass code is:
body { color: var(--color-primary); }
#{var(--variablename)}
This is how you use CSS variables in SCSS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With