Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS overlayed rgba transpanrency

Tags:

html

css

<!doctype>
<html>
<head>
    <style>
        div {
           background:rgba(0,255,255,0.3);
        }
        #div1 {
           width:300px;
           height:300px;           
        }
        #div2 {
           width:200px;
           height:200px;          
        }
    </style>
</head>
<body>
    <div id="div1"><div id="div2"></div></div><br/>
</body>

what is the actually computed transparency of div2? I am confused. It is a unknown number that is not 0.3+0.3 or 0.3+0.3*0.3

like image 843
jinxin ni Avatar asked Jun 23 '15 19:06

jinxin ni


1 Answers

The sum of alpha channels can be calculated as:

alpha_top + alpha_bottom * (1 - alpha_top) =
0.3 + 0.3 * (1 - 0.3) =
0.3 + 0.3 * 0.7 =
0.3 + 0.21 =
0.51

For more explanation, see Alpha Compositing @ wikipedia, which lists the formula as:

formula

or

formula

This is essentially the same formula used by Bogdan Kuštan and it can also be found in this answer by bwoebi.

like image 180
showdev Avatar answered Nov 15 '22 11:11

showdev