我尝试制作一个由四个框组成的网格,每个框的高度和宽度分别为全屏尺寸的 50% 和 50%,如下图所示: http://a.pomf.se/gqnzzs.jpg
我的问题是我无法使 50% 的高度正常工作。如何在每个视口(viewport)大小的 50% 高度处设置 2 个 div 的高度?
HTML:
<div class="wrapper">
<div class="row">
<div class="panel-1">... </div>
<div class="panel-2">...</div>
</div>
<div class="row">
<div class="panel-3">...</div>
<div class="panel-4">...</div>
</div>
</div>
CSS:
.wrapper{
width: 100%;
height: 100%;
display: inline-block;}
.panel-1{
width: 50%;
float: left;
height: 50%;}
.panel-2{
width: 50%;
float: left;
height: 50%;}
.panel-3{
width: 50%;
float: left;
height: 50%;}
.panel-4{
width: 50%;
float: left;
height: 50%;}
非常感谢任何帮助!
问候
最佳答案
在supported browsers,你可以使用 viewport-percentages units , vh
, vw
, vmin
, vmax
.
在这种情况下,只需使用 50vh
,其中 1 个单位等于初始包含 block 高度的 1%。
.panel-1, .panel-2,
.panel-3, .panel-4 {
height: 50vh;
width: 50%;
float: left;
}
值得一提的是,如果您仍想使用基于百分比的单位,则还需要定义所有父元素的高度:
html, body, .wrapper {
height: 100%;
}
.row {
height: 50%;
}
.panel-1, .panel-2,
.panel-3, .panel-4 {
height: 100%;
width: 50%;
float: left;
}
关于html - 响应式 div 视口(viewport)大小的 50% 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29504554/