您可以使用伪元素来做到这一点:before
看到它在这把小提琴 https://jsfiddle.net/4vLg9b7L/1/
#heading {
background-image: url(https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png);
background-repeat: no-repeat;
position: relative;
height:100%;
height: 300px;
width: 960px;
}
#heading::before {
background-color: rgba(240, 216, 171, 0.25);
display: block;
content: '';
position: absolute;
height: inherit;
width: inherit;
}
在这里,由于您需要它作为 header 元素,因此使用 header 而不是单独的背景 div。请注意:before
元素以绝对定位放置,实际元素相对于该元素放置。背景颜色应用于之前的元素,并且带有背景法师的标题在其上渲染。
Edit 1
而不是使用:before
, 我们可以用background-blend-mode:screen
来控制不透明度background-image
并使用background-color
.
看到它在这把小提琴 https://jsfiddle.net/4vLg9b7L/2/.
rgba的alpha通道background-color
可以控制透明度background-image
.
#heading {
background-image: url(https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png);
background-repeat: no-repeat;
background-blend-mode: screen;
background-color: rgba(255, 230, 184, 0.66);
position: relative;
height:100%;
height: 300px;
width: 960px;
}
我使用了谷歌徽标,因为您提供的图像源路径是相对于您的页面的。