简体   繁体   English

想要在背景图像上创建透明的颜色叠加层,但希望叠加层前面的所有内容

[英]Want to create a transparent color overlay over background image but want everything infront of overlay

Basically, I want a background, then a transparent overlay, and then all of my content brought to the very front. 基本上,我想要一个背景,然后是一个透明的覆盖层,然后将我所有的内容带到最前面。 So far I have 到目前为止,我有

#overlay{
    position:absolute;
    left:0;
    right:0;
    top:0;
    bottom:0;
    background-color:grey;
    opacity:0.5;
    }

And I have a <div id="overlay"> surrounding everything in my html document. 我在HTML文档中的所有内容周围都有一个<div id="overlay"> Thanks in advance. 提前致谢。

This would be an easy way of doing it: 这将是一个简单的方法:

 body { width: 100%; height: 100%; } .content { position: relative; width: 500px; height: 500px; background-image: url('http://static.vecteezy.com/system/resources/previews/000/094/491/original/polygonal-texture-background-vector.jpg'); background-size: cover; } .overlay { position: absolute; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); } .inner-content { width: 250px; height: 250px; padding: 20px; background-color: #FFFFFF; margin: 50px 0 0 105px; color: #000000; } 
 <div class="content"> <div class="overlay"> <div class="inner-content">Your inner content</div> </div> </div> 

This sample has a lot of unnecessary code just to make the sample look nice but you should get the point. 该示例有很多不必要的代码,只是为了使示例看起来不错,但您应该明白这一点。

I would use thepios answer but use :before to get rid of the unnecessary overlay div: 我会用thepios答案,但要使用:before摆脱不必要的覆盖div:

 body { width: 100%; height: 100%; } .content { position: relative; width: 500px; height: 500px; background-image: url('http://static.vecteezy.com/system/resources/previews/000/094/491/original/polygonal-texture-background-vector.jpg'); background-size: cover; padding: 50px; } .content:before{ content: ' '; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); } .inner-content { width: 250px; height: 250px; padding: 20px; background-color: #FFFFFF; color: #000000; position: absolute; } 
 <div class="content"> <div class="inner-content">Your inner content</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM