这可能是一个菜鸟问题,但是我在任何地方都找不到答案。我正在编写一个具有视差背景图像的网站,并希望使所述图像相对于其上方的文本而言有点透明,而后者应该是完全不透明的。我遵循了w3school's model(进行了一些更改),并且考虑到在父容器中定义了背景图片,因此它可以正常工作,因此文本继承了图片的不透明度,如bgimg-2
所示。
我试图做的事情,是从摆弄样式表到无济于事,是创建一个新容器section-img
,该容器同时封装了背景和文本,因此它们的样式不会相互重叠。但是,这会使图像的[bgimg-1
)高度等于0。
这里是MRE:
<!DOCTYPE html>
<html>
<style>
body, html {
height: 100%;
margin: 0;
background-color: #282828;
font-family: sans-serif;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.section-img {
position: relative;
min-height: 100%;
}
.bgimg-1 {
background-image: url("https://i.redd.it/v3wjcf1p59841.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
opacity: 1;
z-index: -1;
}
.bgimg-2 {
position: relative;
opacity: 0.6;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-image: url("https://i.redd.it/o1a3xr4b39841.jpg");
min-height: 100%;
}
#title {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 100%;
text-align: center;
color: white;
font-size: 7vw;
letter-spacing: 2vw;
}
.section-text {
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
width: 100%;
text-align: center;
font-weight: bold;
color: white;
letter-spacing: 2vw;
font-size: 3vw;
color: #f7f7f7;
}
</style>
<body>
<div class="section-img">
<div id="title">No background picture here!</div>
<div class="bgimg-1"></div>
</div>
<div class="bgimg-2">
<div class="section-text">I want different opacities :(</div>
</div>
</body>
</html>
实现这两种产品混浊度差异的最明智的方法是什么?
为什么不使用rgba样式,“ rgb”将设置背景颜色,而“ a”命令将设置图像的不透明度(透明度),可以将其设置为0-1,其中0是透明的(不透明度为0%),而1为100%不透明度。我希望这会有所帮助!