我有一个DIV,我想把一个模式作为背景。这个图案是灰色的。所以为了让它更漂亮一点,我想在上面加一个浅色透明的颜色“图层”。下面是我尝试过但没有成功的方法。有没有办法把彩色图层放在背景图像上?

这是我的CSS:

background: url('../img/bg/diagonalnoise.png');
background-color: rgba(248, 247, 216, 0.7);

当前回答

另一个带有SVG的内联覆盖图像(注意:如果你在SVG代码中使用#,你必须urlencode !):

background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"><path fill="rgba(255, 255, 255, 0.4)" d="M0 0h1v1H0z"/></svg>')
                no-repeat center center/cover, 
            url('overlayed-image.jpg') no-repeat center center/cover;

其他回答

下面就是:

.background {
    background:url('../img/bg/diagonalnoise.png');
    position: relative;
}

.layer {
    background-color: rgba(248, 247, 216, 0.7);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

HTML:

<div class="background">
    <div class="layer">
    </div>
</div>

当然,如果.background类中没有其他元素,则需要为它定义宽度和高度

然后你需要一个bg图像的包装元素和bg颜色的内容元素:

<div id="Wrapper">
  <div id="Content">
    <!-- content here -->
  </div>
</div>

还有css:

#Wrapper{
    background:url(../img/bg/diagonalnoise.png); 
    width:300px; 
    height:300px;
}

#Content{
    background-color:rgba(248,247,216,0.7); 
    width:100%; 
    height:100%;
}

你也可以添加不透明度叠加颜色。

而不是做

background: url('../img/bg/diagonalnoise.png');
background-color: rgba(248, 247, 216, 0.7);

你可以:

background: url('../img/bg/diagonalnoise.png');

然后为不透明度颜色创建一个新样式:

.colorStyle{
    background-color: rgba(248, 247, 216, 0.7);
    opacity: 0.8;
}

将不透明度更改为低于1的任意数字。然后将此颜色样式设置为与图像相同的大小。它应该会起作用。

请访问https://stackoverflow.com/a/18471979/193494查看我的回答,了解可能的解决方案的全面概述:

使用线性渐变的多个背景, 多个背景与生成的PNG,或 样式化:在伪元素之后作为次要背景层。

# img-div { 身高:100 vh; 背景图片:url (https://images.pexels.com/photos/46160/field -云的天空-地球- 46160. - jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260”); 背景位置:中心; background-size:封面; 平铺方式:不再重演; 位置:相对; } # overlay-div { Background-color: rgba(0,0,0,0.5); 身高:100 vh; 位置:相对; } < div id = " img-div " > < div id = " overlay-div " > < / div > < / div >