2

Do you know any possibility to combine a heaxadecimal css variable with an alpha value to create a simple "shine through effect"?

e.g. as css variable: --my-color-red: #ff0000;

Now we want in our scss implementation to add this alpha value as a background and following code snippet doesn't work:

background: rgba(var(--my-color-red), 0.5);

Without a css variable it works:

background: rgba(#ff0000, 0.5);

I think maybe the problem is that sass do the rgba implementation at compile time and the css variable is added at runtime afterwards.

Do you know any solution to solve this problem?

JV3
  • 872
  • 2
  • 9
  • 21
  • Does this answer your question? [Sass - Converting Hex to RGBa for background opacity](https://stackoverflow.com/questions/10929458/sass-converting-hex-to-rgba-for-background-opacity) – Peter B Nov 11 '20 at 17:11
  • Sadly, no. That are all solutions based on scss variables. – JV3 Nov 12 '20 at 08:23

1 Answers1

0

You could use a Sass function which converts hexadecimal to RGB:

@function hexToRGB($hex) {
    @return red($hex), green($hex), blue($hex);
}

Then store another version of your CSS variable as RGB:

--my-color-red: #ff0000;
--my-color-red-rgb: #{hexToRGB(#ff0000)};

This will give you the RGB values which you can then tack the alpha value onto within the rgba function:

rgba(var(--my-color-red-rgb), 0.5)

Taken from this article.

Daniel_Knights
  • 7,940
  • 4
  • 21
  • 49