html - 如何设置两个空 div(用于背景图像)的样式,它们恰好一个在另一个之上?
问题描述
我正在尝试将两个空<div>
的 s 直接排在另一个上方,基本上第一个<div>
占据屏幕的上半部分,第二个<div>
占据屏幕的下半部分。两个<div>
s 都包含背景图像,并且需要完美对齐(即顶部的底部<div>
需要与底部的顶部齐平<div>
)才能使我试图创建的效果有意义。
我到目前为止是这样的:
html {
height: 100%;
}
body {
display: block;
text-align: center;
height: 100%;
min-height: 100%;
position: relative;
padding: 0;
margin: 0;
}
.top {
display: block;
background-image: url(photo-top.jpg);
background-position: 50%;
background-size: cover;
height:100%;
min-height: 100%;
width:100%;
top: 0;
margin: 0;
padding: 0;
}
.bottom {
display: block;
background-image: url(photo-bottom.jpg);
background-position: 50%;
background-size: cover;
height:100%;
min-height: 100%;
width:100%;
bottom: 0;
margin: 0;
padding: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div class="top"></div>
<div class="bottom"></div>
</body>
</html>
这在顶部和底部图像之间放置了一个巨大的空白区域。我该如何解决?
解决方案
You can use the following solution using 50vh
for each <div>
as height:
html {
height: 100%;
}
body {
display: block;
text-align: center;
height: 100%;
min-height: 100%;
position: relative;
padding: 0;
margin: 0;
}
.top {
display: block;
background-color:red;
background-image: url(https://picsum.photos/id/1025/200/300);
background-position: 50%;
background-size: auto 100%;
background-repeat:no-repeat;
height: 50vh;
width: 100%;
margin: 0;
padding: 0;
}
.bottom {
display: block;
background-color:blue;
background-image: url(https://picsum.photos/id/1025/200/300);
background-position: 50%;
background-size: auto 100%;
background-repeat:no-repeat;
height:50vh;
width:100%;
margin: 0;
padding: 0;
}
<div class="top"></div>
<div class="bottom"></div>
In case there is no other content below the two <div>
elements you can use absolute positioned elements:
html {
height: 100%;
}
body {
display: block;
text-align: center;
height: 100%;
min-height: 100%;
position: absolute;
padding: 0;
margin: 0;
top:0;
bottom:0;
left:0;
right:0;
}
.top {
display: block;
background-color:red;
background-image: url(https://picsum.photos/id/1025/200/300);
background-position: 50%;
background-size: auto 100%;
background-repeat:no-repeat;
height: 50vh;
width: 100%;
margin: 0;
padding: 0;
position:absolute;
top:0;
bottom:50%;
left:0;
right:0;
}
.bottom {
display: block;
background-color:blue;
background-image: url(https://picsum.photos/id/1025/200/300);
background-position: 50%;
background-size: auto 100%;
background-repeat:no-repeat;
height:50vh;
width:100%;
margin: 0;
padding: 0;
position:absolute;
top:50%;
bottom:0;
left:0;
right:0;
}
<div class="top"></div>
<div class="bottom"></div>
推荐阅读
- r - “管道”、“点”和“美元”运算符的连接似乎在 R 中起作用?
- npm - 如何在 JupyterLab 中安装 nteract Data Explorer 作为扩展?
- ios - 在终端上获取 URL 后如何将数据传递给 tableview?斯威夫特 5
- conv-neural-network - 应用 2 个 conv 2d 层时的图像输出
- mysql - 将 MySQL /var/lib/mysql 移动到共享卷
- c++ - 测试“stream >> char”有什么作用?
- angularjs - 对 AngularJs 基于组件的模式适应 TypeScript 感到困惑
- sql-server - 如何在 SQL Server -PIVOT 中获取最近 30 天交易的最后 30 分钟记录?
- python - 蛮力只对某些密码有效,而对其他密码无效?
- aws-lambda - AWS API Gateway 将 Content-Type 标头添加到 OPTIONS 请求