Have a child opacity different then parent opacity with CSS

后端 未结 5 1198
花落未央
花落未央 2021-01-20 11:10

Here are my box classes

.rectangle-box {
    width: 200px;
    height: 30px;
    background: #808080;
    opacity: 0.3;
    float: right;
}

.re         


        
5条回答
  •  被撕碎了的回忆
    2021-01-20 12:01

    You can't the opacity cannot be greater than parent

    but you can use two methods

    I have used rgba rgba(0,0,0,0.0)

    .rectangle-box {
      width: 200px;
      height: 30px;
      background: rgba(128, 128, 128, 0.3);
      float: right;
      position: relative;
    }
    
    .rectangle-red {
      width: 65px;
      height: 30px;
      background: #ff4742;
      opacity: 1;
      float: left;
    }

    Or the second method i have used :pseudo element to add a background

    .rectangle-box {
      width: 200px;
      height: 30px;
      float: right;
      position: relative;
    }
    
    .rectangle-box:after {
      content: '';
      opacity: 0.3;
      background: #808080;
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      top: 0;
      z-index:-1;
    }
    
    .rectangle-red {
      width: 65px;
      height: 30px;
      background: #ff4742;
      opacity: 1;
      float: left;
    }

提交回复
热议问题