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

标签 html css 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>

谢谢!

最佳答案

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

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>

关于html - 在同一个 div 上叠加颜色和背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47441509/

相关文章:

css - 表格内的表格 css

javascript - 滚动时让右div在父div中滑动

css - 背景图像和从文件夹图像重复

html - 需要帮助前端用户使用 Merchello 电子商务产品搜索功能

html - 什么是 HTML 链接 Markdown 使内部链接更容易的好语法?

html - 允许用户更改网页布局

html - 另一个 div 和文本上的背景图像

html - 文件无法编辑 Github 页面

c# - 使用 C# 在 webbrowser 控件中获取 HTML 元素的绝对位置

java - Android Studio为所有xml设置背景图片