I'm trying to fill the vertical space of a flex item inside a flexbox:
.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;
}
<div class="container">
<div class="flex-1"></div>
<div class="flex-2">
<div class="flex-2-child"></div>
</div>
</div>
And here's the JSFiddle
flex-2-child
doesn't fill the required height except in the two cases where:
flex-2
has a height of 100% (which is weird because a flex item has a 100% by default + it is buggy in Chrome)flex-2-child
has a position absolute which is also inconvenient
This doesn't work in Chrome or Firefox currently.
I have answered a similar question here.
I know you have already said
position: absolute;
is inconvenient but it works. See below for further information on fixing the resize issue.Also see this jsFiddle for a demo, although I have only added webkit prefixes so open in Chrome.
You basically have 2 issues which I will deal with separately.
position: relative;
on the parent of the child.position: absolute;
on the child.overflow-y: auto;
on the scrollable div.height: 0;
See this answer for more information on the scrolling issue.
This is my solution using css+
First of all if first child (flex-1) should be 100px shouldn't be flex. In css+ in fact you can set flexible and/or static elements (columns or rows) and your example become as easy as this:
container css:
and obviously include css+ 0.2 core
hear the fiddle
An idea would be that
display:flex;
withflex-direction: row;
is filling thecontainer
div with.flex-1
and.flex-2
, but that does not mean that.flex-2
has a defaultheight:100%;
even if it is extended to full height and to have a child element (.flex-2-child
) withheight:100%;
you'll need to set the parent toheight:100%;
or usedisplay:flex;
withflex-direction: row;
on the.flex-2
div too.From what I know
display:flex
will not extend all your child elements height to 100%.A small demo, removed the height from
.flex-2-child
and useddisplay:flex;
on.flex-2
: http://jsfiddle.net/2ZDuE/3/html
css
http://jsfiddle.net/2ZDuE/750/
I suppose that Chrome's behavior is more consistent with the CSS spec (though it's less intuitive). According to Flexbox spec, the default
stretch
value ofalign-self
property changes only the used value of the element's "cross size property" (height
, in this case). And, as I understand the CSS2.1 spec, the percentage heights are calculated from the specified value of the parent'sheight
, not its used value. The specified value of the parent'sheight
isn't affected by any flex properties and is stillauto
.Setting explicit
height: 100%
makes it formally possible to calculate the percentage height of the child, just like settingheight: 100%
tohtml
makes it possible to calculate the percentage height ofbody
in CSS2.1.I found the solution by myself, suppose you have the CSS below:
In this case, setting the height 100% will not work, so what I do is setting the
margin-bottom
rule toauto
, like:and the child will be aligned to the topmost of the parent, you can also use the
align-self
rule anyway if you prefer.