【发布时间】:2018-03-30 23:15:46
【问题描述】:
我想一个接一个地显示 div #botone、#bottwo 和 #botthree。我还希望在#botone div 上将背景图像调整为 100% 宽度和比例高度。
现在最后两个 Div 出现在 #botone Div 后面,#botone div 将 49px 添加到来自 .topnav Div 的垂直位置。
我还想将.topnav Div 固定在顶部。
** body {
margin: 0px 0px;
}
/* Add a black background color to the top navigation */
.topnav {
background-color: #000;
overflow: hidden;
height: 49px;
}
/* Style the links inside the navigation bar */
.topnav a {
float: right;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.block {
float: left;
width: 40px;
}
.blocka {
float: right;
width: 250px;
}
#botone {
position: absolute;
height: 100%;
width: 100%;
background-image: url("arc.jpg");
background-size: cover;
}
#bottwo {
background-color: blue;
height: 400px;
width: 100%;
}
#botthree {
background-color: yellow;
height: 600px;
width: 100%;
}
<div class="topnav">
<div class="block">
<a href="#oi">Oi</a>
</div>
<div class="blocka">
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
</div>
</div>
<div id="botone">testtext
</div>
<div id="bottwo">testtext
</div>
<div id="botthree">testtext
</div>
【问题讨论】:
-
#botone是绝对定位的,而其他则不是。这意味着#botone不在文档流中,不会影响其他2个div的放置 -
@zgood 谢谢。但是如何使背景图像适应#botone 上窗口的完整高度和宽度。如果位置不是绝对的,height 属性似乎不起作用。
-
如果您想要可视窗口(视口)的高度,可以尝试将高度设置为
100vh。 -
@zgood。谢谢!有效。现在只有我想固定在页面顶部的 topnav div。位置:固定;好像没用。