-1

I have a requirement where the user can change the theme by setting his own colors from the provided drop down. Based on this color me as a developer will apply different shades to all the sections as below

Say, if the user picked up Green then, I want to use Green for the primary header, and Green with opacity 0.6 for table headers, and Green with opacity 0.5 for active/selected table row.

I have tried the below format, passing a variable to rgba is not affecting the background color.

CSS

:root {
    --color: #008000;
    --alpha: 0.5;
}

#primary-header{
    background-color: rgba(var(--color));
}

#table-header{
    background-color: rgba(var(--color), var(--alpha));
}
Maciej
  • 21,919
  • 3
  • 20
  • 23
vinod
  • 260
  • 3
  • 12

1 Answers1

1

If i understand your request properly, you can manipulate the css using javascript.
here is a working example to change color and opacity of the css variables.

<!DOCTYPE html>
<html>
  <head>
    <style>
    :root {
    --currentColor: orange;
    --op: 0.9;
}
a {
  color: var(--currentColor)
  opacity: var(--op);
}
div {
  width: 100px;
  height: 100px;
  background-color: var(--currentColor);
  opacity: var(--op);
}
    </style>
  </head>
  <body>
    <select id="combo" onchange="changeColor()">
      <option>orange
      </option>
      <option>green
      </option>
      <option>red
      </option>
      <option>blue
      </option>
    </select>
    <div>
    </div>
    <select select id="combo2" onchange="changeColor()">
      <option>0.9</option>
      <option>0.8</option>
      <option>0.1</option>
      <option>0.5</option>
    </select>
    <script>
      function changeColor()
      {
        // get the color value from the select control
        var color = document.getElementById("combo").value;
        // apply css change
        document.documentElement.style.setProperty('--currentColor', color);
        // get the opacity value from the select control
        var color = document.getElementById("combo2").value;
        // apply css change
        document.documentElement.style.setProperty('--op', color);
      }
    </script>
  </body>
</html>
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52