CSS overlapping elements & opacity issue

后端 未结 3 1159
孤独总比滥情好
孤独总比滥情好 2020-12-11 15:59

I\'ve stumbled upon an issue with rendering two overlapping elements with opacity = .5. The elements are exactly the same and positioned absolutely. One is on top of the oth

相关标签:
3条回答
  • 2020-12-11 16:27

    There are three items being added together:

    • White background at 100%
    • First picture at 50%
    • Second picture at 50%

    The first two make the first picture much lighter prior to mixing with the second picture.

    0 讨论(0)
  • 2020-12-11 16:31

    Try and think of it like percentage sales. It's a bit different, but the analogy gives sense of what's happening. When a $10 item is 80% off, then you take off an additional 20%, it's' not 100% off (80% + 20%). You calculate final price like this:

    $10 * (1 - 0.8)  = $2 * (1 - 0.2) = $1.60.
    

    50% opacity means, 50% of the light is blocked. So when you stack two 50% opacity elements, that means 50% of the light is blocked and 50% more light is blocked by the additional layer. Since only 50% light is coming through the first layer, only 50% of that additional light is blocked. So the calculation would be:

    50% + (50% * 50%) = 75% opacity.
    

    DEMO

    .num2 {
        opacity: 0.75;
    }
    
    0 讨论(0)
  • 2020-12-11 16:33

    Short answer: Opacity is not a linear function, so it doesn't add.

    Longer answer: here or here

    0 讨论(0)
提交回复
热议问题