html - 当容器具有动态高度时设置 flex 元素的百分比高度

标签 html css flexbox

我有一个简单的包装器,其中有 2 个 div 元素。

我希望第一个获得 85% 的高度,第二个获得仅 15% 的高度。

当我为包装器设置固定高度时它会起作用。尽管遗憾的是我的 wrapper 具有动态高度。

你知道我怎样才能做到这一点吗?

我还提供了一个插件:http://plnkr.co/edit/HQpahfmRasij8Zougjkn?p=preview

代码:

.outer{
  flex: 1;
  display: flex;
  flex-direction: column;
  flex-basis: 0;
  /* if i set the fixed height everthing works
     though i do want a dynamic height
  height: 800px; */
}
.main {
  background-color: blue;
  display: flex;
  flex: 0 0 85%;
  max-height: 85%;
  flex-direction: row;
  height: 400px;
}
.navigator {
  background-color: red;
  display: flex;
  flex: 0 0 15%;
  max-height: 15%;
  flex-direction: row;
  height: 400px;
}
<div class="outer">
  <div class="main" >
    <!-- this container should have 85% of the outer containers height -->
  </div>
  <div class="navigator" >
    <!-- this container should have 15% of the outer containers height -->
  </div>
</div>

最佳答案

您可以在没有 flex 的情况下进行初始(外部)布局,因为当不需要它时我看不到要点。

不过,要求是相同的,.outer 的父级需要一个高度,可以继承,也可以设置。

html, body {
  height: 100%;
  margin: 0;
}
.outer {
  height: 100%;
}
.main {
  background-color: blue;
  height: 85%;
  display: flex;            /* this is for main's children */
  flex-direction: row;      /* this is for main's children */
}
.navigator {
  background-color: red;
  height: 15%;
  display: flex;            /* this is for nav's children */
  flex-direction: row;      /* this is for nav's children */
}
<div class="outer">
  <div class="main" >
    <!-- this container should have 85% of the outer containers height -->
  </div>
  <div class="navigator" >
    <!-- this container should have 15% of the outer containers height -->
  </div>
</div>

关于html - 当容器具有动态高度时设置 flex 元素的百分比高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35179802/

相关文章:

html - display flex 和 display block 的 CSS 问题

css - 像 Flexbox 元素一样的 block 级元素

html - 语义标记 : Combining Description List and Details/Summary Tag for Accordion

css - 我可以使用 CSS "unicode-range"来指定整个(第三方)页面的字体吗?

css - 样式元素 'input[type="提交“]' and ' 按钮”与其他元素具有不同的宽度

css - Angular w/Flexbox app-root 不是 100% 宽

css - 将有序的元素数组拆分为 HTML 中的交替列

php - 在验证某些 $_POST 变量是否正确后,我希望关闭窗口。我如何使用 JQuery 执行此操作?

html - 尽管使用边距 : 0 auto,Firefox 仍不使表格居中

javascript - 我有一个标准表格,里面有文字。我如何垂直对齐它?