how to handle 'double opacity' of two overlapping divs

前端 未结 5 1104
时光取名叫无心
时光取名叫无心 2021-01-23 16:19

I have two divs, both with 0.6 opacity. I need them to overlap but retain their opacity and not create a new combined opacity level. I can\'t use an image.

EDIT -- The

5条回答
  •  广开言路
    2021-01-23 17:03

    Revised Answer

    This fiddle is compatible with IE9 and resolves the duplication of background needed in my original answer. It does use pseudoelements to generate the circle. This solution spins off pixelass's "pacman" idea, only instead of using the newer background gradient css to generate, it uses the older (and little used or understood) clip property to make the circle in two parts. This solved the issue of your circle not being "centered" at the corner.

    #foo {
        height:150px;
        width:250px;
        background: rgba(0, 0, 0, 0.6);
        position:absolute;
        left:40%;
        top:20%;
    }
    
    #bar {
        height:40px;
        width:40px;
        position:absolute;
        top:-15px;
        left:-15px;
        line-height: 40px;
    }
    
    #bar:before,
    #bar:after {
        content: '';
        display: block;
        background: rgba(0, 0, 0, 0.6);
        border-radius: 40px;
        width: 100%;
        height: 100%;
        position: absolute;
        z-index: -1;
        top: 0;
        left: 0;
    }
    
    #bar:before {
        clip: rect(0 40px 15px 0);
    }
    
    #bar:after {
        clip: rect(15px 15px 40px 0);
    }
    

    Original Answer

    You can do this (see fiddle). It pushes the circle below and "overlays" the portion that overlaps with a pseudoelement to reestablish the background color of the body:

    body{background:green;}
    
    #foo{
    height:150px;
    width:250px;
    background:rgba(0, 0, 0, 0.6);
    position:absolute;
    left:40%;
    top:20%;
    }
    
    #bar{
    height:40px;
    width:40px;
    background:rgba(0, 0, 0, 0.6);
    border-radius:40px;
    position:absolute;
    top:-15px;
    left:-15px;
    z-index: -1;
    }
    
    #bar:after {
        content: '';
        display: block;
        background: green;
        position: absolute;
        right: 0;
        bottom: 0;
        width: 25px;
        height: 25px;
    }
    

提交回复
热议问题