【发布时间】:2016-11-30 04:51:57
【问题描述】:
我已经构建了一个我在 CodePen 中遇到的问题的示例,其中我有一些文本,我试图使其在其所在的背景下更易于阅读;背景图像正在使用background-image 设置,因此您不能直接对其应用不透明度(AFAIK),所以我尝试在其上放置全宽背景颜色并设置不透明度以使背景变暗 - 但是这有导致其上的内容消失。
CodePen: http://codepen.io/gutterboy/pen/GqGgmG (禁用.bg:before 以查看内容)
HTML:
<div class="bg">
<div class="container">
<ul class="left">
<li>something here</li>
<li>something here</li>
<li>something here</li>
<li>something here</li>
</ul>
<ul class="right">
<li>something here</li>
<li>something here</li>
<li>something here</li>
<li>something here</li>
</ul>
</div>
</div>
CSS:
.bg {
width: 100%;
background: #20334c url("some_image.jpg") center center no-repeat;
background-size: cover;
height: 550px;
overflow: hidden;
position: relative;
}
.bg:before {
display: block;
content: '';
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.5);
}
.container {
width: 960px;
margin: 0 auto;
position: relative;
}
ul {
position: absolute;
top: 75px;
font-size: 2em;
font-weight: bold;
z-index: 9999;
color: #fff;
}
ul.left {
left: 50px;
}
ul.right {
right: 50px;
}
为什么要将伪 :before 元素应用于 .bg 类隐藏其顶部的内容?
【问题讨论】:
-
将
position: absolute添加到您的:before
标签: html css pseudo-element