div元素的不透明度受图像不透明度的影响

时间:2020-07-23 14:49:19

标签: html css opacity

我有一个前端项目,其中有一个square item over an image。我的正方形被假定为全红色,但损失opacity。 这是正方形在我页面中的样子,而不是全红色: enter image description here 我的代码:

.html,.body{
    background-size: cover;
}

body{
    overflow-x: hidden;
    margin:0;
    background: rgba(0, 0, 0, 0.1);

}
.welcome-container{
    position: relative;
    top:0px;
    left: 0px;
    width:100%;
    height:550px;
    margin:0px;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    



}

.welcome-pic{
    position: relative;
}


.welcome-pic img{
    background: linear-gradient( rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5) ), url('file:///Volumes/Animus/Jon/Dropbox/website/hellcity.jpg');
}

.ds-square{
    background-color:red;
    height: 50px;
    width:50px;
    position: absolute;
    top: 2px;
    left: 2%;
    opacity:1;

}

.welcome-pic img {
    width:100%;
    height:550px;
    opacity: 0.8;
}

.inside-pic{
    position:absolute;
    top:30%;
    left:50%;
    font-size: 60px;
    transform: translate(-50%, -50%);
}

.inside-pic h3{
    font-size:18px;
}
<div class = "welcome-container">
      <div class = "welcome-pic">
      //my square 
        <div class="ds-square">
        </div>
        <img src = "IMAGES/welcome_pic.jpg" alt="#">
        <div class = "inside-pic">
         
          FLY WITH  DS <br/> AIRLINES
          <h3>Flights from or towards Athens ! Fly secure and comfortable with us! </h3> 
        </div>
      </div>
    </div>

在正方形上尝试opacity:1;不会改变结果。感谢您在此简单任务中的帮助。预先谢谢你

1 个答案:

答案 0 :(得分:1)

.html,.body{
    background-size: cover;
}

body{
    overflow-x: hidden;
    margin:0;
    background: rgba(0, 0, 0, 0.1);

}
.welcome-container{
    position: relative;
    top:0px;
    left: 0px;
    width:100%;
    height:550px;
    margin:0px;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    



}

.welcome-pic{
    position: relative;
}


.welcome-pic img{
    background: linear-gradient( rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5) ), url('file:///Volumes/Animus/Jon/Dropbox/website/hellcity.jpg');
}

.ds-square{
    background-color:rgba(255,0,0,0.4);
    height: 50px;
    width:50px;
    position: absolute;
    top: 2px;
    left: 2%;
    opacity:1;

}

.welcome-pic img {
    width:100%;
    height:550px;
    opacity: 0.8;
}

.inside-pic{
    position:absolute;
    top:30%;
    left:50%;
    font-size: 60px;
    transform: translate(-50%, -50%);
}

.inside-pic h3{
    font-size:18px;
}
<div class = "welcome-container">
      <div class = "welcome-pic">
      //my square 
        <div class="ds-square">
        </div>
        <img src = "IMAGES/welcome_pic.jpg" alt="#">
        <div class = "inside-pic">
         
          FLY WITH  DS <br/> AIRLINES
          <h3>Flights from or towards Athens ! Fly secure and comfortable with us! </h3> 
        </div>
      </div>
    </div>