Search code examples
csslinear-gradientscss-variables

Darken a color specified in a CSS variable


I'd need to darken a color specified in a CSS variable.

I have this CSS code:

background:linear-gradient(to right, 
                           var(--mainContainer-background) 80%,
                           @gray-light 100%);

CSS variable --mainContainer-background can be changed by user on-the-fly with a bit of JS, but I'd want to set a darken variation of this color in my background gradient, for example I'd want that gradient start from a color that is 20% darker of one stored in --mainContainer-background.

Is possible in some way?


Solution

  • You may consider another gradient above with a transparent black color:

    .box {
      min-height:50px;
      margin:20px;
      background: 
       linear-gradient(to right,rgba(0,0,0,0.6),transparent 80%),
       linear-gradient(to right, var(--color,blue) 80%, grey 100%);
    }
    <div class="box" style="--color:red;">
    </div>
    <div class="box" style="--color:pink;">
    </div>
    <div class="box" >
    </div>