Have a child opacity different then parent opacity with CSS

后端 未结 5 1249
花落未央
花落未央 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:11

    Here is a nice and neat way using pseudo elements.

    With this you can as well add images and svg to each background which gives a lot of options.

    If you need other elements within each box, you'll need the second inner div.

    .rectangle-box {
      width: 200px;
      height: 30px;
      float: right;
      position: relative;
    }
    
    .rectangle-box:before {
      content: "";
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      position: absolute;
      background: #808080;
      opacity: 0.3;
    }
    .rectangle-box:after {
      content: "";
      top: 0;
      left: 0;
      width: 65px;
      height: 100%;
      position: absolute;
      background: #ff4742;
      opacity: 1;
    }

提交回复
热议问题