让孩子的不透明度与CSS的父不透明度不同

时间:2015-12-30 20:45:47

标签: html css

以下是我的box课程

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

.rectangle-red {
    width: 65px;
    height: 30px;
    background: #ff4742;
    opacity: 1;
    float: left;
}

在HTML中:

<div class="rectangle-box">
    <div class="rectangle-red"></div>
</div>

DEMO:https://jsfiddle.net/uq6ectfc/1/

我需要rectangle-red不透明度1rectangle-box 0.3。但它坚持父母的不透明。

我该如何解决?

5 个答案:

答案 0 :(得分:8)

opacity不能大于父

但你可以使用两种方法

我使用了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;
}
<div class="rectangle-box">
  <div class="rectangle-red"></div>
</div>

或者我使用:pseudo元素添加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;
}
<div class="rectangle-box">
  <div class="rectangle-red"></div>
</div>

答案 1 :(得分:5)

使用RGBA代替十六进制。使用不透明度:影响子元素而rgba不会

.rectangle-box {
        width: 200px;
        height: 30px;
        background-color: rgba(128,128,128, 0.3);
        float: right;

    }

    .rectangle-red {
        width: 65px;
        height: 30px;
        background-color: rgba(255,71,66, 1);
      float: left;
    } 

答案 2 :(得分:3)

更好的结构方法是创建一个包含两个框的div。这样,每个盒子的不透明度都不会相互干扰。

<div class="container">
    <div class="rectangle-box"></div>
    <div class="rectangle-red"></div>
</div>

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

.rectangle-red {
    width: 65px;
    height: 100%;
    background: #ff4742;
    opacity: 1;
    float: left;
}

答案 3 :(得分:1)

你不能

你所能做的就是在.rectangle-box绝对(我的情况)或亲戚或任何你想要的低透明度.lower-opacity内创建元素,这样他们就是兄弟姐妹而不会打扰彼此的不透明属性

.rectangle-box {
    width: 200px;
    height: 30px;
    float: right;
    position: relative;
}
.lower-opacity{
    position: absolute;
    opacity: 0.3;
    width: 100%;
    height: 100%;
    background: #808080; //**EDITED** BACKGROUND NOW WILL BE TRANSPARENT
}
.rectangle-red {
    width: 65px;
    height: 30px;
    background: #ff4742;
    opacity: 1;
    float: left;
}


<div class="rectangle-box">
    <div class="lower-opacity"></div>
    <div class="rectangle-red"></div>
</div>

答案 4 :(得分:1)

这是使用伪元素的一种漂亮而巧妙的方式。

有了这个,您还可以为每个背景添加图像和svg,这提供了很多选项。

如果您需要每个框中的其他元素,则需要第二个内部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;
}
<div class="rectangle-box">
</div>