csssass

Convert rgb to rgba using scss and assign to css property


I need to convert from rgb to rgba using scss' rgba function, and assign the result to a css property (for use with a style library outside my control).

Both these approaches fail:

.test1 { --foo: rgba(#f00, .5); }    // .test1 { --foo: rgba(#f00, .5) }
$col: rgba(#f00, .5);
.test2 { --foo: $col; }              // .test2 { --foo: $col }

How does one do this?


Solution

  • You are looking for relative colors

    body {
      --foo: rgb(from #f00 r g b / .5);
      
      background: var(--foo)
    }

    Note that rgba() is an obsolete function, rgb() contains the alpha value

    Note: The rgba() functional notation is an alias for rgb(). They are exactly equivalent. It is recommended to use rgb(). ref