css - 如何使 Flex 中的父容器和其他子容器成长为最长的子容器之一

标签 css flexbox

每当蓝色 div 中的内容增长时,我需要容器和绿色 div 增长为与蓝色相同的大小。

.container {
  background: grey;
  height: 200px;
  flex-direction: column;
  padding: 10px;
  display: flex;
}

.normal {
  background: green;
}

.wide {
  width: 2500px;
  background: blue;
  flex: 1 0 auto;
}
<div class="container">
  <div class="normal">Normal</div>
  <div class="wide">Wide</div>
</div>

CodePen

最佳答案

改为考虑inline-flex

.container {
  background: grey;
  height: 200px;
  flex-direction: column;
  padding: 10px;
  display: inline-flex;
  min-width:100%; /*to make sure it behave like flex if the content is small*/
  box-sizing:border-box;
}

.normal {
  background: green;
}

.wide {
  width: 2500px;
  background: blue;
  flex: 1 0 auto;
}
<div class="container">
  <div class="normal">Normal</div>
  <div class="wide">Wide</div>
</div>

相关:Why does the outer <div> here not completely surround the inner <div>?

关于css - 如何使 Flex 中的父容器和其他子容器成长为最长的子容器之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59662040/

相关文章:

html - 使用 CSS flex 的两个全高页面

css - 我无法让我的页脚全宽

jquery - 使用百分比宽度 Jquery 设置 div 的动画宽度

jquery - 标题标签在 jQuery 3.0 及更高版本中显示为文本

html - Div 和 Text 一起调整大小以保持比例

css - 使用 ng-repeat 和 google materials flex box(带包装?)

html - 删除 flex 布局中列之间的间隙

css - 防止在 flexbox 子元素中换行

HTML Flex 布局在 Internet Explorer 中看起来有所不同

javascript - 如何在不影响内部 anchor 的情况下使 li 可点击?