在同一个div上叠加颜色和背景图像

时间:2017-11-22 18:21:38

标签: html css css3 background-image background-color

这可能吗?要在一个div上设置背景颜色背景图像吗?如:

div {
  color: white;
  background-color: hsla(15, 92%, 13%, 0.79);
  height: 500px;
  background-image: url('http://via.placeholder.com/500x500');
}
<div>
  <p>Content goes here</p>
</div>

谢谢!

2 个答案:

答案 0 :(得分:1)

你可以使用这样的伪元素:

div {
  position: relative;
  color: white;
  background-image: url('http://via.placeholder.com/500x500');
  height: 500px;
}

div:before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: hsla(15, 92%, 13%, 0.79);
  z-index: 0;
}

p {
  position: relative;
  z-index: 2;
}
<div>
  <p>Content goes here</p>
</div>

答案 1 :(得分:0)

div {
  color: white;
  background: url('http://via.placeholder.com/500x500');
  background-color: hsla(15, 92%, 13%, 0.79);
  background-blend-mode: overlay;
  height: 500px;
}
<div>
  <p>Content goes here</p>
</div>

这是你需要的吗?