4

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?

1 Answer 1

8

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; */
}
Sign up to request clarification or add additional context in comments.

4 Comments

how do you do with the result of a function directly?
just found out you could wrap functions in interpolation brackets as well! :)
I have looked for a solution for this for months!
Very helpful, thanks. Found some further info here on why this is required: sass-lang.com/documentation/style-rules/declarations/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.