Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot assign SCSS values to css custom properties (css variables)

To ensure readability and maintainability, I would like to declare several values like colors for example, but only once.

The problem I have is that it seems impossible to assign the result of a sass function or even simple variables directly to css variable.

.some-selector {
    --my-css-variable: $my-color; // nope
    --my-css-variable: mySassFunction(); // nope
    --my-css-variable: transparentize($my-color, .5); // nope
    --my-css-variable: copy-pasted-value; // alright
}

I can't seem to find any answer on search engines, always finding irrelevant topics at best. Could you help me?

like image 652
Romain Vincent Avatar asked Oct 15 '25 20:10

Romain Vincent


1 Answers

Try with interpolation #{...}

$my-color: #fff;

@function myFunction() {
    @return #000;
}

.some-selector {
    --my-css-variable: #{$my-color}; 
    /* output: --my-css-variable: #fff; */
    --my-css-variable: #{myFunction()}; 
    /* output: --my-css-variable: #000; */
}
like image 146
Fabrizio Calderan Avatar answered Oct 18 '25 14:10

Fabrizio Calderan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!