【问题标题】:100% Height without scrollbars (parent and nested childs with flexbox)没有滚动条的 100% 高度(带有弹性框的父级和嵌套子级)
【发布时间】:2014-04-23 22:12:43
【问题描述】:

我喜欢没有任何滚动条的 100% 高度。在我的示例中,浏览器似乎计算了整个高度。子元素如何获得父元素的 100% 高度。

html

<div class="wrapper">
<div class="header"></div> //<- Here is the problem
<div class="content">
    <div class="element">
       <div class="element-child1"></div>
       <div class="element-child2"></div>
    </div>
<div/>

css

  html, body, .wrapper {
    width: 100%;
    height: 100%;
    margin: 0;
  }

  .wrapper {
    display: flex;
    flex-direction: column;
  }

  .wrapper > .header {
    flex: 0 0 40px;
    background-color: black;
  }

  .wrapper > .content {
    align-self: stretch;
    flex: 1;
    background: red;
  }

  .element {
     align-self: stretch;
     display: flex;
     flex-direction: column;
     background-color: yellow;
  }

  .element-child1 {
     flex: 0 0 20px;
     background-color: blue;
  }

  .element-child2 {
     flex: 1;
     background-color: green;
  }

滚动条示例:http://jsfiddle.net/zyYPv/

【问题讨论】:

标签: html css flexbox


【解决方案1】:

您应该能够使用flex-grow 属性:

HTML

<div class="wrapper">
    <div class="header"></div>
    <div class="content">
        <div class="element-child1"></div>
        <div class="element-child2"></div>
    <div/>
</div>

CSS

html, body, .wrapper {
    width: 100%;
    height: 100%;
    margin: 0;
}

.wrapper {
    display: flex;
    flex-direction: column;
}

.wrapper > .header {
    flex: 0 0 40px;
    background-color: black;
}

.wrapper > .content {
    flex-grow: 1;    
    display: flex;
    background: red;
    flex-direction: column;
}

.element-child1 {
    flex: 0 0 20px;
    background-color: blue;
}

.element-child2 {
    flex-grow: 1;
    background-color: green;
}

flex-grow example

替代品

calc example

display:table example

【讨论】:

  • 我已经这样做了。 Flexbox 解决方案会更好。不需要任何支持。只为我自己。如果 flexbox 没有答案,你就接受了。
【解决方案2】:

Demo Fiddle

CSS

  html, body, .wrapper {
      width: 100%;
      height: 100%;
      margin: 0;
      padding:0;
  }
  .wrapper > .header {
      height: 40px;
      background-color: black;
  }
  .content {
      position:absolute;
      top:40px;
      bottom:0;
      background: red;
      width:100%;
  }
  .element {
      height: 100%;
      background-color: yellow;
  }
  .element-child1 {
      height: 20px;
      background-color: blue;
  }
  .element-child2 {
      background-color: green;
      position:absolute;
      top:20px;
      bottom:0;
      width:100%;
  }

【讨论】:

  • 谢谢,但这不是我要的。我不能只删除父元素。
  • @MR.ABC - 抱歉,您的问题不清楚,请参阅上面的更新答案
  • 是的,有时很难。 Element-child2 不再是 100%。它不填充内容。
  • @MR.ABC - 仍然不清楚您的意思,请将overflow-y:scroll 添加到.element-child2
  • 我只需要一个 100% 的元素。 overflow-y:scroll 会破坏下面的布局。
猜你喜欢
  • 2018-02-07
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 2018-04-29
  • 1970-01-01
  • 2012-10-06
相关资源
最近更新 更多