I got a layout to work in CSS but I don't understand why it works. CSS experts, please shed light on what's going on here.
My goal was to have a layout like this:
The salient features are:
- Fixed height header and footer at the top and bottom of the window
- A content area taking up the full space between the header and footer
- The content area has its own scrollbar (i.e. it doesn't push the footer down past the bottom of the screen).
I made it happen using a combination of css flex box and a div inside the content div with 0% height. Against my expectation, the 0% height div stretches to the full available height.
Here's a JSFiddle: http://jsfiddle.net/2wunC/ (it doesn't look quite right because the outer height:100%
doesn't make sense inside the jsfiddle container).
HTML
<div id="everything">
<div id="header">fixed-height header</div>
<div id="workspace">
<div class="docstretch"><div class="docwindow">
filler text<br>filler text<br>filler text<br>...
</div></div>
</div>
<div id="footer">fixed-height footer</div>
</div>
CSS
#everything {display:-webkit-flex;-webkit-flex-flow:column;-webkit-align-items:stretch;height:100%;}
#header {height:2em;background:lightgray;}
#footer {height:2em;background:darkgray;}
#workspace {-webkit-flex:1 0 auto;display:-webkit-flex;-webkit-flex-flow:row;}
.docstretch {background:#05c;color:white;width:300px;overflow-y:scroll;}
.docwindow {height:0%;} /* why does this work? */
My question: the nested divs (docstretch and docwindow) with the inner div having height:0%
created the content div I wanted, taking up the full 'interior space' of the page and having its own scrollbar. But why? And is this a safe solution?