Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change alpha of an HSL color if the color comes from a CSS custom property?

I have a list of HSL colors coded as CSS custom properties and I want to manipulate their alpha channel.

What I have tried:

:root {
  --color: hsl(25, 33%, 93%);
}

.test { box-shadow: ... hsla(var(--color), 1); }

In the web browser debugger, checking the styles tab, I observed that the CSS rule is valid as it was not marked as invalid. However, switching to the computed tab and expanding the box-shadow, shows that the computed result is none. So this CSS syntax seems to be correct, but it appears that browsers (Chrome 112 and Firefox 112) are not rendering it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What can I do to add an alpha channel to HSL colors coded as CSS custom properties?

P.S. There is this messy SASS solution, but I wish not to use it because it requires too much boilerplate.

>Solution :

Yes you can by using hsla and removing the brackets from the variable.

:root {
  --green: 120, 100%, 50%;
}

.box {
  width: 100px;
  height: 100px;
  background: hsla(var(--green), 0.5)
}
<div class="box"></div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading