响应式 div 视口大小的 50% 高度

Responsive divs 50% height of viewport size

我尝试制作一个由四个框组成的网格,每个框的高度和宽度分别为全屏尺寸的 50% 和 50%,如下图所示: http://a.pomf.se/gqnzzs.jpg

我的问题是我无法使 50% 的高度正常工作。如何在每个视口大小的 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, you can use viewport-percentages units,vh,vw,vmin,vmax.

在这种情况下,只需使用 50vh,其中 1 个单位等于初始包含块高度的 1%。

Example Here

.panel-1, .panel-2,
.panel-3, .panel-4 {
    height: 50vh;
    width: 50%;
    float: left;
}

值得一提的是,如果您仍想使用基于百分比的单位,则还需要定义所有父元素的高度:

Updated Example

html, body, .wrapper {
    height: 100%;
}
.row {
    height: 50%;
}
.panel-1, .panel-2,
.panel-3, .panel-4 {
    height: 100%;
    width: 50%;
    float: left;
}

flexbox 解决。 Caniuse

没有浮动,没有明确的修正 - 简单明了;

*,
*:before,
*:after {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
html,
body {
  padding: 0;
  margin: 0;
  height: 100%;
}
body {
  background-color: #333;
  display: -webkit-box;
  display: -moz-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: box;
  display: flex;
  -webkit-box-flex: 1;
  -moz-box-flex: 1;
  -o-box-flex: 1;
  box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
  -webkit-box-orient: horizontal;
  -moz-box-orient: horizontal;
  -o-box-orient: horizontal;
  -webkit-box-lines: multiple;
  -moz-box-lines: multiple;
  -o-box-lines: multiple;
  -webkit-flex-flow: row wrap;
  -ms-flex-flow: row wrap;
  flex-flow: row wrap;
}
.flex__item {
  display: -webkit-box;
  display: -moz-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: box;
  display: flex;
  -webkit-box-flex: 1;
  -moz-box-flex: 1;
  -o-box-flex: 1;
  box-flex: 1;
  -webkit-flex: 0 0 50%;
  -ms-flex: 0 0 50%;
  flex: 0 0 50%;
  height: 50%;
  background-color: #777;
  border: 1px solid #000;
}
.flex__item:nth-child(1) {
  background-color: #9aa0a8;
}
.flex__item:nth-child(2) {
  background-color: #a7c4b5;
}
.flex__item:nth-child(3) {
  background-color: #a9d8b8;
}
.flex__item:nth-child(4) {
  background-color: #beffc7;
}
<div class="flex__item"></div>
<div class="flex__item"></div>
<div class="flex__item"></div>
<div class="flex__item"></div>

您需要设置以下内容: html { height:100%; width:100%;} body { width:100%;height:100%;}

然后 "div" 将与 "height:50%" 一起使用。