首页 > 解决方案 > 如何使 CSS 布局具有受孩子最大高度限制的流动行高?

问题描述

布局:

有一个容器可以填充 100% 的窗口高度。里面有两个.parent盒子(红色填充),其中包含.child元素(黄色填充)。子元素有min-heightmax-height设置。

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

.container {
    display: grid;
    height: 100%;
    gap: 20px;
}

.parent {
    background: #f00;
}

.nested {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.child {
    height: 100%;
    min-height: 100px;
    max-height: 150px;

    background: #ff0;
}

.another-child {
    flex: none;
    height: 50px;

    background: #0f0;
}
<div class="container">
  <div class="parent">
    <div class="child"></div>
  </div>
  <div class="parent">
    <div class="nested">
      <div class="another-child"></div>
      <div class="child"></div>
    </div>
  </div>
</div>

目标:

如果内部有空间,则使.parent框仅垂直增长直到子元素,如果没有,则缩小至子元素。父块红色背景不应可见。max-height.containermin-height

标签: csslayoutresponsive-design

解决方案


推荐阅读