The problem seems to stem from the fact that you're combining both the height
and flex-basis
properties in the same declaration block. It appears to create a conflict. In my answer below I've removed the heights and used flex-basis
instead. You'll note a couple of other adjustments, as well.
Change:
(1)
<div class="green-border"
style="flex: 1 1 auto; display: flex; flex-direction: row; width: 100%; height: 100%;" >
TO
<div class="green-border"
style="flex: 1 1 100%; display: flex; flex-direction: row; width: 100%;" >
notes: removed height
; used flex-basis
instead;
(2)
<div style="flex: 1 1 auto; width: 100%; height: 100%;">
TO
<div style="flex: 1 1 100%; width: 100%; display: flex;">
notes: removed height
; used flex-basis
instead; established nested flex container;
(3)
<div style="background-color: #ff6500; width: 100%; height: 100%;">center</div>
TO
<div style="background-color: #ff6500; width: 100%; flex: 1 1 100%;">center</div>
notes: removed height
; used flex-basis
instead; added flex
property;
Lastly, the blue border wasn't showing due to a typo.
(4)
clsas="blue-border"
TO
class="blue-border"
With the adjustments above, the layout renders in Chrome like this:
All boxes lie within their containers.
DEMO
Applying 100% height to nested, non-flex elements
In most cases, when using percentage heights on child elements, a percentage height must also be specified for the parent element and all ancestor elements, up to and including the root element.
html, body { height: 100%; }
I have explained the reason for this here:
But a close look at the spec reveals an exception:
The percentage is calculated with respect to the height of the
generated box's containing block. If the height of the containing
block is not specified explicitly (i.e., it depends on content
height), and this element is not absolutely positioned, the value
computes to 'auto'.
Note the part: ... and the element is not absolutely positioned.
Hence, try this on the "center" box:
Change:
(5)
<div style="background-color: #ff6500; width: 100%; flex: 1 1 100%;">center</div>
TO
<div style="background-color: #ff6500; width: 100%; flex: 1 1 100%; position: relative;">
<div style="border: 5px solid yellow; position: absolute; height: 100%; width: 100%;">
center</div>
</div>
Notes:
- added
position: relative
to establish nearest positioned ancestor for absolute positioning
- created new, nested, non-flex
div
container
- applied absolute positioning to new
div
with height: 100%
.
With absolute positioning you do not need to apply percentage heights to parent containers.
DEMO