Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS variable not working within SCSS when I use var and rgba

Tags:

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?

like image 786
Novak254 Avatar asked Apr 23 '20 22:04

Novak254


People also ask

Can I use CSS variables in SCSS?

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.

Can SCSS variable change dynamically?

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.

Should I use CSS variables or SCSS variables?

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.


1 Answers

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>
like image 96
Ash Avatar answered Oct 02 '22 14:10

Ash