如何让flexbox儿童100%的父母身高?

Raz*_*Raz 393 css css3 flexbox

我正在尝试填充flexbox内的flex项目的垂直空间.

.container {
  height: 200px;
  width: 500px;
  display: flex;
  flex-direction: row;
}
.flex-1 {
  width: 100px;
  background-color: blue;
}
.flex-2 {
  position: relative;
  flex: 1;
  background-color: red;
}
.flex-2-child {
  height: 100%;
  width: 100%;
  background-color: green;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="flex-1"></div>
  <div class="flex-2">
    <div class="flex-2-child"></div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

这是JSFiddle

flex-2-child 没有填写所需的高度,除非在以下两种情况下:

  1. flex-2 高度为100%(这很奇怪,因为弹性项目默认为100%+ Chrome中存在错误)
  2. flex-2-child 有一个绝对的位置也很不方便

这在目前的Chrome或Firefox中不起作用.

Bre*_*tin 271

在这里回答了类似的问题.

我知道你已经说过position: absolute;不方便但是有效.有关修复调整大小问题的详细信息,请参阅下文.

另请参阅这个jsFiddle进行演示,虽然我只在Chrome中添加了webkit前缀.

你基本上有2个问题,我将分别处理.

  1. 让灵活项目的孩子填充高度100%
    • 设置position: relative;在孩子的父母身上.
    • 坐落position: absolute;于孩子.
    • 然后,您可以根据需要设置宽度/高度(我的样本中为100%).
  2. 在Chrome中修复调整大小滚动"quirk"
    • 穿上overflow-y: auto;可滚动的div.
    • 可滚动div必须具有指定的显式高度.我的样本已经有100%的高度,但如果没有应用,你可以指定height: 0;

有关滚动问题的详细信息,请参阅此答案.

  • 点1)在Chrome中完美运行,Firefox不需要绝对位置. (10认同)
  • 这不是一个可接受的解决方案。在我多年的编程生涯中,我了解到对于某些事情,您只能非常小心地使用绝对位置,因为元素可能无法在其他浏览器中正确呈现。另外,答案没有得到解决,因为他在标题中提到了 Flexbox,所以这个答案是错误的。 (4认同)

Dav*_*rey 212

如果我理解正确,你想要flex-2-child填充其父级的高度和宽度,以便红色区域完全被绿色覆盖?

如果是这样,您只需要设置flex-2即可使用flexbox:

.flex-2 {
    display: flex;  
}
Run Code Online (Sandbox Code Playgroud)

然后告诉flex-2-child变得灵活:

.flex-2-child {    
    flex: 1;
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/2ZDuE/10/

原因是flex-2-child不是flexbox项目,但它的父项是.

  • @NeilMonroe如果你只对一个孩子使用`align-self:stretch`,你仍然可以使用`align-items:center`. (10认同)
  • 请注意,如果您使用"align-items:center",您将无法使用此修复程序,因为您的项目现在将变为等高. (7认同)
  • 嗨@David,我尝试了你的解决方案,它适用于 flex-direction: 行布局,它似乎不适用于 flex-direction: 列布局,或者我在代码中有任何错误的地方。你能帮忙检查一下为什么在这个小提琴http://jsfiddle.net/78o54Lmv/中,内部盒子不会占据其父盒子的完整高度吗? (3认同)

B T*_*B T 143

与David Storey的答案类似,我的工作是:

.flex-2 {
    display: flex;  
    align-items: stretch;
}
Run Code Online (Sandbox Code Playgroud)

或者align-items: stretch,你可以align-items只使用align-self你想拉伸的项目.

  • 如何对齐已拉伸到中间垂直对齐的 div 内的文本? (6认同)
  • `stretch`:这应该是选择的答案。 (5认同)
  • 是的,你应该添加删除 `height: 100%` (2认同)

Ily*_*syn 23

我认为Chrome的行为与CSS规范更加一致(尽管它不太直观).根据Flexbox规范,属性的默认stretch仅更改元素的"交叉大小属性"(在本例中)使用.而且,据我了解CSS2.1规范,百分比高度是根据父项的指定值计算的,而不是其使用的值.父级的指定值不受任何弹性属性的影响,但仍然存在.align-selfheightheightheightauto

设置显式height: 100%使得正式可以计算子项的百分比高度,就像设置height: 100%html可以计算bodyCSS2.1中的百分比高度一样.

  • Chrome的行为与*Chrome Team对CSS规范的解释更为一致.虽然可以通过这种方式进行解释,但有更好的方法可以解释它,其中任何一种方法都不会让我们陷入这个集市,烦人和令人讨厌的行为.他们应该考虑更多. (7认同)
  • 究竟!要将其付诸实践,只需将`height:100%`添加到`.flex-2`即可.这是[jsfiddle](http://jsfiddle.net/2ZDuE/114/). (2认同)
  • 将垂直 flexbox 容器的子级设置为 `height: 100%` 在 Chrome 中给我带来了非常奇怪的结果。这似乎导致“指定高度”被设置为*容器的*总高度*,而不是元素本身的高度,因此当其他元素相对于它计算大小时会导致不必要的滚动。 (2认同)

use*_*501 10

我自己找到了解决方案,假设您有以下CSS:

.parent {
  align-items: center;
  display: flex;
  justify-content: center;
}

.child {
  height: 100%; <- didn't work
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,设置高度100%将不起作用,所以我所做的是将margin-bottom规则设置为auto,如:

.child {
  margin-bottom: auto;
}
Run Code Online (Sandbox Code Playgroud)

并且孩子将与父母的最顶层对齐,align-self如果您愿意,您也可以使用该规则.

.child {
  align-self: flex-start;
}
Run Code Online (Sandbox Code Playgroud)

  • 一个不错的技巧,但问题是:如果 child 是侧边栏并且它有背景颜色,则边距空间仍然是白色的。 (3认同)

Ber*_*rci 9

另一种方法(其他选项不适合我的情况,因为我需要一个更通用的解决方案,我真的想position: absolute尽可能避免使用,并且display: flex由于align-items: stretch其他syling而在我的情况下无法使用):

  1. 有一个父母与display: flex
  2. 也给你的元素显示flex,align-self: stretch并且margin-top: 0+margin-bottom: 0

最小可重现示例:

.parrent {
  display: flex;
}
.full-height-just-with-bg-collor {
  width: 100%;
  display: flex;
  align-self: stretch;
  // this can be just 0 or you can set just the top and bottom values
  margin: 0 2px;
  background-color: green;
}
Run Code Online (Sandbox Code Playgroud)
<div>
  <div class="parrent">
    <div class="full-height-just-with-bg-collor"></div> 
    textForHeight
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


Gio*_*osK 7

这也可以align-self: stretch;在我们想要拉伸的元素上解决。

有时需要在 Flexbox 设置中只拉伸一项。

.container {
  height: 200px;
  width: 500px;
  display: flex;
  flex-direction: row;
}
.flex-1 {
  width: 100px;
  background-color: blue;
}
.flex-2 {
  position: relative;
  flex: 1;
  align-self: stretch;
  background-color: red;
}
.flex-2-child {
  background-color: green;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="flex-1"></div>
  <div class="flex-2">
    <div class="flex-2-child"></div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


小智 6

.parent {
    display: flex;
}

.child {
    min-height: 100%;
}
Run Code Online (Sandbox Code Playgroud)


rma*_*002 5

一个想法是display:flex;with用andflex-direction: row;填充containerdiv ,但这并不意味着它有默认值,即使它扩展到全高。.flex-1.flex-2.flex-2height:100%;

要拥有一个.flex-2-child带有 的子元素 ( ) height:100%;,您还需要将父元素设置为height:100%;或在div 上使用display:flex;with 。flex-direction: row;.flex-2

据我所知,display:flex不会将所有子元素的高度扩展到 100%。

一个小演示,删除了高度.flex-2-child并用于display:flex;.flex-2http ://jsfiddle.net/2ZDuE/3/


Car*_*Kay 5

.container {
    height: 200px;
    width: 500px;
    display: -moz-box;
    display: -webkit-flexbox;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: -moz-flex;
    display: flex;
    -webkit-flex-direction: row;
    -moz-flex-direction: row;
    -ms-flex-direction: row;
    flex-direction: row;
}
.flex-1 {
   flex:1 0 100px;
    background-color: blue;
}
.flex-2 {
    -moz-box-flex: 1;
    -webkit-flex: 1;
    -moz-flex: 1;
    -ms-flex: 1;
    flex: 1 0 100%;
    background-color: red;
}
.flex-2-child {
    flex: 1 0 100%;
    height: 100%;
    background-color: green;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
    <div class="flex-1"></div>
    <div class="flex-2">
        <div class="flex-2-child"></div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/2ZDuE/750/

  • 应该有一个解释。 (2认同)

Tia*_*oLr 5

.parent {
    display: flex;
    align-items: center;
}

.child {
    align-self: stretch;
}
Run Code Online (Sandbox Code Playgroud)

根据之前的答案,该解决方案适用于全高度柔性元素。