Anton Harald Anton Harald - 3 months ago 17
CSS Question

split space of a fixed size container equally. a case for flexbox?

How can a HTML/CSS structure be designed, that splits a fixed size container horizontally into three parts. The first part should be as tall as it's content needs. The second and the third part would share the remaining space fifty-fifty - regardless of their contents. If the size of their contents exceeds this limitation, the section should be scrollable.

The HTML part of it is trivial: A

div
container with three
div
's as children.

I tried to solve this with flexbox - however there might be a better choice for this:

The css part:

#container {
position: absolute;
width: 100%; height: 100%;
display: flex;
flex-direction: column;
}

#item1 { flex: 0 0 auto; }
#item2 { flex: 1 1 auto; }
#item3 { flex: 1 1 auto; }


Unfortunately this only works if the contents of item 2 or 3 is not too large.

See this fiddle for a bit more detailed implementation of the problem.



body {
margin: 0;
overflow: hidden;
}
* {
box-sizing: border-box;
}
#container {
position: absolute;
width: 100%;
height: 100%;
border: 2px solid green;
display: flex;
flex-direction: column;
}
#item1 {
flex: 0 0 auto;
background-color: Bisque;
}
#item2 {
flex: 1 1 auto;
background-color: DarkOrange;
}
#item3 {
flex: 1 1 auto;
background-color: MediumAquaMarine;
}

<div id="container">
<div id="item1">I'll be as tall as my content takes.</div>
<div id="item2">From the rest, I'll take exactly 50%. No matter how short or long my content is. If needed there should be scrollbars.</div>
<div id="item3">I'll take the other 50% of the rest!
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
</div>
</div>




Answer

Yes, you can use flex. Here's a little improvement for your code. Item1 doesn't need to have a flex rule and item2 and item3 will have flex: 1.

I also added the overflow-y: auto; rule to make it scrollable.

Example

#item1 {background-color: Bisque ; }
#item2 { flex: 1; background-color: DarkOrange ; overflow-y: auto;}
#item3 { flex: 1; background-color: MediumAquaMarine ; overflow-y: auto;}