【问题标题】:Responsive divs 50% height of viewport size响应式 div 视口大小的 50% 高度
【发布时间】:2015-06-12 19:33:53
【问题描述】:

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

我的问题是我无法让 50% 的高度工作。如何将 2 个 div 的高度设置为视口大小的 50%?

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%;}

非常感谢任何帮助!

问候

【问题讨论】:

    标签: html css responsive-design grid


    【解决方案1】:

    supported browsers, 中,您可以使用viewport-percentages unitsvhvwvminvmax

    在这种情况下,只需使用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;
    }
    

    【讨论】:

    • 完美运行!并且似乎适用于所有主流浏览器,包括 IE9。我会说这已经足够好了!特别是在 Windows 10 不再使用 IE 的情况下。非常感谢您的快速回复!
    【解决方案2】:

    flexbox 解决。 Caniuse

    没有浮动,没有clearfixes - 简单明了;

    *,
    *: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>

    【讨论】:

    • 香草!提供更多浏览器支持。非常感谢!现在我只需要将它与基础集成。
    【解决方案3】:

    您需要设置以下内容: html { 高度:100%;宽度:100%;} 身体{宽度:100%;高度:100%;}

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

    【讨论】:

      猜你喜欢
      • 2014-12-08
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 2017-03-30
      • 1970-01-01
      • 2012-04-26
      相关资源
      最近更新 更多