I'm trying to use two nested flex boxes to make a basic 2-column layout that fills the available space (here, hard-coded to a 300px by 300px yellow container), but the columns end up too tall (spilling outside the yellow). A summary of the code below:
- The outer flex box (
.columns
) is horizontal, and appropriately splits up the horizontal space in half. - Each inner flex box contains two elements, a div containing an SVG element (
.figure
) and a div for a caption. - The
.figure
element has analign-self: stretch
. Removing this fixes the problem.
* { box-sizing: border-box; }
.columns {
width: 100%;
display: flex;
align-items: center;
}
.column {
flex-basis: 0;
flex-grow: 1;
align-self: stretch;
max-height: 100%;
display: flex;
flex-direction: column;
align-items: center;
border: cyan solid;
}
.figure {
flex-basis: 0;
flex-grow: 1;
align-self: stretch;
/* doesn't help: */
display: flex;
flex-direction: column;
}
svg {
width: 100%;
height: 100%;
border: purple solid 3px;
}
.caption {
border: magenta solid;
}
<div style="width: 300px; height: 150px; background: yellow">
<div class="columns" style="height: 150px">
<div class="column">
<div class="figure">
<svg viewBox="0 0 1 1"></svg>
</div>
<div class="caption">Caption</div>
</div>
<div class="column">
<div class="figure">
<svg viewBox="0 0 1 1"></svg>
</div>
<div class="caption">Caption</div>
</div>
</div>
</div>
I'm hopefully missing something simple to fix this, but having tried many additions (min-height: 0
in various places, for example), I keep getting the caption spilling outside the yellow box, without a manual workaround like <div class="column" style="height: calc(100% - 25px)">
to give space for the caption.
In case it matters, I'm testing on Chrome.