I have searched around for this but none seems to work.
I am working on Angular and have my scss variables on the root file, styles.scss in the :root pseudo-selector. The following works in my component scss;
:root {--color-primary-green:#00e676}
background-color:var(--color-primary-green);
When I try to use rgba, the color disappears i.e
background-color: rgba(var(--color-primary-green), 0.5);
How can I go around this?
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. Sass variables are imperative, which means if you use a variable and then change its value, the earlier use will stay the same.
CSS Variables to the Rescue CSS variables is a standard which enables you to have variables in CSS. 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.
The advantage of CSS variables is undisputed. They can be transformed and overridden whereas SCSS variables can not. CSS variables simplify creating color theme based sites like this right one right here.
In your style variables file include both the hex and rgba versions of your colours. Then you can use the rgb value when it is required.
:root {
--color-primary: #2dd36f;
--color-primary-rgb: 45, 211, 111;
}
.hex-bg {
background-color: var(--color-primary);
}
.rgba-bg {
background-color: rgba(var(--color-primary-rgb), 0.5);
}
div {
padding: 8px;
margin: 8px;
}
<div class="hex-bg">
background with hex value
</div>
<div class="rgba-bg">
background with rgba value
</div>
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