我正在尝试使背景图像的透明度达到70%而不会影响它前面的文本(在同一个div中)。
HTML
#home {
opacity: 0.7;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-image: url("...");
min-height: 100%;
overflow: hidden;
}
div .welcome {
background-size: cover;
vertical-align: middle;
text-align: center;
font-family: 'Lobster', cursive;
font-weight: 900;
font-size: 60px;
color: black;
margin: auto;
}

<div id="home" class="section">
<p class="welcome"><strong>Hello,<br>My name is Sean</strong></p>
</div>
&#13;
答案 0 :(得分:1)
使用伪元素和position:absolute
请注意,home
的直接子项元素需要一个位置,例如我设置为content
,或者您需要设置z-index: -1
伪元素,如果没有,伪将分层在顶部
body {
background: blue
}
#home {
position: relative;
min-height: 100%;
overflow: hidden;
}
#home::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
opacity: 0.5;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-image: url(http://placehold.it/200/f00); /* image is red but blue shine
through so becomes purple */
}
.content {
position: relative;
color: white;
}
<div id="home">
<div class="content">
Content not affected
</div>
</div>