css - 限制 flex 盒布局中的 flex 元素高度

标签 css

我正在尝试创建一个 flexbox 布局,如 jsfiddle 所示. 这是 HTML:

<div class="container">
  <div class="header">
    header
  </div>
  <div class="content">
  </div>
  <div class="footer">
    footer
  </div>
</div>

这是 CSS:

.container {
  background-color: green;
  height: 300px;
  max-height: 300px;
  display: flex;
  flex-direction: column;
}

.header {
  background-color: yellow;
  flex: 0 0 50px;
}

.content {
  background-color: blue;
  flex: 0 0 90%;
  overflow: scroll;
}

.footer {
  background-color: red;
  flex: 0 0 40px;
}

它有效,但我也会限制整个布局的大小不超出“.container”元素的固定高度。

我怎样才能做到这一点?

谢谢, 杰拉德

最佳答案

您只需要通过向其添加 flex: 1; 属性,使 .content 填充容器内的整个允许空间。

工作示例:

var i;
for (i = 0; i < 100; i++) {
  $(".content").append("<div>hello " + i + "</div>");
}
.container {
  background-color: green;
  height: 300px;
  max-height: 300px;
  display: flex;
  flex-direction: column;
  border:3px solid black;
}

.header {
  background-color: yellow;
  flex: 0 0 50px;
}

.content {
  background-color: blue;
  flex: 1;
  overflow: scroll;
}

.footer {
  background-color: red;
  flex: 0 0 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="header">
    header
  </div>
  <div class="content">
  </div>
  <div class="footer">
    footer
  </div>
</div>

jsFiddle:https://jsfiddle.net/azizn/0aumg6b0/

关于css - 限制 flex 盒布局中的 flex 元素高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37359279/

相关文章:

html - 无论大小如何,在 Safari 中的输入中垂直居中文本

css - Chrome 网络应用程序大小和位置

javascript - 使用鼠标悬停文本链接激活 div 悬停

css - 根据变量突出显示表格行

css - 带有列表更改宽度的 Bootstrap 3.0 词缀

html - 如何在点击标签时实现单选元素的选择

jquery - Bootstrap Datetimepicker 显示不正确

php - Wordpress Bootstrap 折叠菜单不显示链接

javascript - 在 JavaScript 中创建的自定义元素

javascript - 如何为所有 3 个单选按钮保留 500px 的左边距