html - 使用 :before CSS3 pseudo-selector 使背景图像变暗

标签 html css twitter-bootstrap bootstrap-4

我的目标是使用一些 CSS 技巧向大屏幕背景图像添加不透明度为 50% 的深色叠加层,使其更暗。

我尝试使用 :before CSS 选择器在大屏幕前面添加深色矩形,这在普通大屏幕类上工作正常,但是在流体大屏幕上使用相同的方法,会使容器中的内容和按钮被覆盖且无法访问。

我的 HTML 和 CSS 代码。目前我正在使用 Bootstrap 4 类。

.jumbotron {
  position: relative;
  background: url(https://source.unsplash.com/7bzbyafVTYg/1920x1080) no-repeat bottom center / cover; 
}
.jumbotron:before {
  position: absolute;
  content: '';
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 51, 153, 0.8);
}
.jumbotron .container {
  padding: 100px 0;
}
.jumbotron h1,
.jumbotron .lead {
  color: #fff;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="jumbotron jumbotron-fluid mb-0 text-center">
	<div class="container">
		<h1 class="display-4">MAIN PAGE</h1>
		<a class="btn btn-primary btn-lg mt-" href="#sec-pricing" role="button">Learn more</a>
	</div>
</div>

最佳答案

尝试添加 z-index

.jumbotron {
  position: relative;
  background: url(https://source.unsplash.com/7bzbyafVTYg/1920x1080) no-repeat bottom center / cover; 
}
.jumbotron:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 51, 153, 0.5);
}
.jumbotron .container {
    z-index: 10;
    position: relative;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<div class="jumbotron jumbotron-fluid mb-0 text-center">
    <div class="container">
        <h1 class="display-4" style="color:var(--white);">MAIN PAGE</h1>
        <a class="btn btn-primary btn-lg mt-" href="#sec-pricing" role="button">Learn more</a>
    </div>
 </div>

关于html - 使用 :before CSS3 pseudo-selector 使背景图像变暗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49644492/

相关文章:

javascript - click.function 和 Bootstrap 的轮播按钮之间的冲突

javascript - HTML将一个html文件中的一个div的内容加载到另一个html文件中

html - 内联表单 twitter bootstrap

css - 尝试实现与 airbnb.com 相同的轮播

javascript - 幻灯片上的图像向下跳跃

html - 使用 CSS 对三个相等的框进行响应式垂直对齐

jquery - 具有空单元格的表格行不显示边框

javascript - 提交按钮有时会以 html 形式执行两次操作

html - canvas.toBlob() 方法在不同浏览器中的稳定性如何?

javascript - 使用 JSBarcode 库无法正确打印条码