为背景图像添加黑色叠加层

pat*_*g94 18 html css

我已经看了几个关于这个的SO帖子:我想通过添加叠加来使当前背景图像变暗.

#header1 {
  background: url("http://lorempixel.com/image_output/cats-q-c-640-480-10.jpg");
  background-position:center center;
  position: relative;
  background-size: cover;
  padding-bottom:5em;
}
.overlay {
  background-color: rgba(0, 0, 0, 0.7);
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  position: relative;
  z-index: 1;
}
Run Code Online (Sandbox Code Playgroud)
<div class="header">
  <div class="overlay">
    <div class="jumbotron" id="header1">
      <h1>Hello</h1>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

也许我不明白如何使用z-index,或者我在这里遗漏了一些东西.用于着色的较暗背景未显示出来.有什么指针吗?

var*_*uru 15

使用线性渐变

要使背景变暗,请参阅此codepen和此链接

<div class="bg-img"></div>

.bg-img {
  width: 100%;
  height: 100%;
  background: url('http://alexcarpenter.me/img/banner.jpg') center center no-repeat;
  background-size: cover;

  &:before {
    content: '';
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background-image: linear-gradient(to bottom right,#002f4b,#dc4225);
        opacity: .6; 
  }
}
Run Code Online (Sandbox Code Playgroud)


HTM*_*oob 10

#header1 {
    background: url("https://www.random.org/analysis/randbitmap-rdo.png");/*Random image I grabbed*/
    
    
    background-size: cover;
 
  
}

h1 {
    color: white;
    background-color: rgba(0, 0, 0, 0.7);
    padding-top: 10px;
    padding-bottom: 100px;
    padding-left: 20px;
  padding-right: 20px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="header">
    <div class="overlay">
        <div class="jumbotron" id="header1">
            <h1>Hello</h1>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

按照预期,h1充当额外的可视层,其填充覆盖#header1.

第二个解决方案是将原始背景图像添加到.header中,并将h1中的样式赋予#overlay,并进行一些调整,这也应该可以解决问题.

还有另一个可能的解决方案(类似于第二个),您可以将背景图像添加到叠加层,并从我给#header1或.jumbotron的示例中获取h1样式

除了第一个解决方案之外,您还应该能够通过添加背景颜色来添加额外的图层来覆盖.我不确定它会如何影响背景但是从我猜测它应该只添加一层额外的颜色.

这是我使用这种技术的个人示例.