Browsers automatically evaluate hex or hsl colors to rgb when setting via element.style.background?

As per the spec:

If the value is translucent, the computed value will be the rgba() corresponding one. If it isn’t, it will be the rgb() corresponding one.

Meaning that no matter what is your input, the computed value always results in either rgb or rgba.

So, answering your question: yes, it is standard behaviour and no, you can’t use hex or hsl as it will be computed back to rgba.

Leave a Comment