I have a flexbox container with 3 sections. The idea is that each section will be like an accordion and expand, but with any one of them expanded, I need the sections content to overflow vertically only if it would push the whole container past 100% height of the parent, while keeping within 100% of the height of the flexbox's container's container.
Here's a snippet of what I've tried with percentage heights.
html, body {
height: 100%;
margin: 0;
}
#container {
width: 100%;
height: 100%;
background-color: gray;
display: flex;
flex-direction: column;
}
#a {
flex: 0 1 auto;
}
#b {
flex: 2 1 auto;
}
#c {
flex: 0 1 auto;
}
#s2 {
max-height: 100%;
overflow: auto;
background-color: lightblue;
}
<div id="container">
<div id="a"><div>Section 1</div></div>
<div id="b">
<div>Section 2</div>
<div id="s2">
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
</div>
</div>
<div id="c"><div>Section 3</div></div>
</div>
What I'd like is to be able to get something like the following snippet, but without specifying explicit heights of the parent element or flex items.
html, body {
height: 100%;
margin: 0;
}
#container {
width: 100%;
height: 100%;
background-color: gray;
display: flex;
flex-direction: column;
}
#a {
flex: 0 1 auto;
}
#b {
flex: 2 1 auto;
}
#c {
flex: 0 1 auto;
}
#s2 {
max-height: 300px;
overflow: auto;
background-color: lightblue;
}
<div id="container">
<div id="a"><div>Section 1</div></div>
<div id="b">
<div>Section 2</div>
<div id="s2">
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
</div>
</div>
<div id="c"><div>Section 3</div></div>
</div>
Note that Section 3 takes up the slack remaining from Section 2's inferred flex-basis. I want Section 2 to grow to 100% and then overflow. Is this even possible with flexbox?
I was overwhelmed with similar-sounded questions, the closest of which I believe are:
Make a div fill the height of the remaining screen space - This is very similar but my question adds the "needs to scroll if it overflows 100%".
Child with max-height: 100% overflows parent - I think this is the root of the problem. Without having a set parent height, it cannot calculate the height to overflow from.
How to make a flexbox container to scroll? - Again a flex-basis of pixel units is needed. :(.
I'm able to using display: tables
or other solutions. After typing this out I'm thinking that a javascript solution will be needed.