A designer always provides CSS using opacity rather than actual color values. Does this affect rendering performance in the browser?
Which renders faster, on an always white background, where I don't actually care about transparency.
<span>Hello</span>
span {color: black; opacity: 0.7;}
span {color: rgba(0, 0, 0, 0.7);}
span {color: #b3b3b3;}
My gut says opacity is slower (despite being put in the GPU) as now at render time the browser has to take into account the background, so any changes will cause it to repaint the object because of this transparency, while a statically colored object will never change.
Opacity sets the opacity value for an element and all of its children; While RGBA sets the opacity value only for a single declaration.
The CSS opacity property is used to specify the transparency of an element. In simple word, you can say that it specifies the clarity of the image. In technical terms, Opacity is defined as degree in which light is allowed to travel through an object.
Changing the opacity of the background color only To achieve this, use a color value which has an alpha channel—such as rgba. As with opacity , a value of 1 for the alpha channel value makes the color fully opaque. Therefore background-color: rgba(0,0,0,. 5); will set the background color to 50% opacity.
Transparency using RGBA The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque). Tip: You will learn more about RGBA Colors in our CSS Colors Chapter.
I've just made a simple HTML containing around 50k lines of span
.
Then I used Google performance option to check the rendering progress.
Using span {color: black; opacity: 0.7;}
:
Using span {color: rgba(0, 0, 0, 0.7);}
:
And finally using span {color: #b3b3b3;}
:
So, as you've guessed, using opacity is slower by a fair margin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With