scroll bar on div with overflow:auto and percentage height

techdog picture techdog · Jun 12, 2013 · Viewed 20.2k times · Source

Is it possible to create a div that adjusts to the size of the browser and is also scrollable? I want to use overflow:auto on the div and percentage height of 90%.

The structure of the page is

<body style="height:100%">
<div id=header style="height:100%">headerinfo</div>
<div id=content style="height:100%;overflow:auto">content which is replaced dynamically</div>
</body>

Will overflow:auto work without a known px height somewhere in the hierarchy?

Answer

Chris Spittles picture Chris Spittles · Oct 10, 2013

In answer to your question, yes overflow:auto will work, but you will need height: 100% on the HTML tag as well:

html,body {    
    height:100%;
    margin: 0;
}
#header {
    height: 100%;
}
#content {
    height: 100%;
    overflow: auto;
}

The way your markup is structured though will produce two divs both the same height as the viewport one on top of the other. Is that what you intended?

If so here is a jsFiddle that illustrates your example. I've tweaked the markup and added additional content so that the content div overflows as you require.

http://jsfiddle.net/chrissp26/WsNjm/