【发布时间】:2016-12-02 23:47:00
【问题描述】:
我正在尝试用另一张图片掩盖 html 正文背景图片。我使用了 z-index 但无法正常工作。我是遗漏了什么还是无法理解我在做什么。
参见下面的示例代码:
body, html {
margin: 0;
/* The image used */
background-image: url('http://lorempixel.com/output/nature-q-c-1176-907-10.jpg');
/* Full height */
height: 100%;
/* Create the parallax scrolling effect */
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
}
#main-content {
background-image: url('http://lorempixel.com/output/food-q-c-640-633-1.jpg');
z-index: 10;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
position: relative;
}
.content {
/*height: 700px;*/
height: 100vh;
background:rgba(255,255,255,0.5);
/*margin-top: 20%;*/
}
<div id="main-content">
<div class="content">something in here..</div>
<div class="content">something in here..</div>
<div class="content">something in here..</div>
</div>
另外,
main-content内部是带有100vh的 div 和background opacity
主要内容图像应位于所有内容之上,包括具有背景不透明度的 content div。
【问题讨论】:
-
你没有给 div 任何宽度或高度,尝试添加它们
-
@Akshay,它有效!但无法让它在我的实际页面上运行。很奇怪
-
你是怎么设置宽高的?喜欢
100%或px? -
@Akshay,以百分比表示。是的 100% 宽度和高度。我使用我的样本对其进行了测试,并且可以正常工作。也许是因为其他 div 使用了 vh?
-
如果你将
100%高度赋予一个div,我相信它的所有父母也应该有100%高度