Click to See Complete Forum and Search --> : DIV to take up remaining height of viewport.


Lypheus
02-12-2008, 03:56 PM
I'd like to find a way to stack two divs vertically, where the top div is a fixed height and the bottom dynamic in order to fill the remaining page height. I know this can be done by using position:absolute and using hidden divs to pad things out, but there must be a cleaner approach to this problem out there.

Looking for something very specific to start here, just have 96px tall header div and then being able to add a div below to expand and fill the remaining height of the page (or containing div), resizing as needed to fill out the page as it is resized.

Below is something that does not work but shows basically what I want to achieve here. As you can see by saving this and previewing it in a browser, the lower (body) div causes scrolling on the webpage which is what i'm trying to avoid here.

Tried using position:absolute and Z indexes with some success, but this is a big kludgy when you consider i'll be dynamically adding draggable windows inside this div, which serves as a "workspace" of sorts.

Javascript might work here I suppose, the approach being to capture viewport resize events and calculate the new height via JS, but this seems fragile for diff browsers and kludgy as well.

Anyone have some ideas?

<html>
<head>
<title>DDS</title>
</head>

<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
border: 0;
}

div#canvas {
background-color: lightyellow;
width: 100%;
height: 100%;
}

div#header {
background-color: lightgray;
width: 100%;
height: 196px;
vertical-align: top;
}

div#body {
background-color: darkgray;
width: 100%;
height: 100%;
}
</style>

<body>
<div id="canvas">
<div id="header">head</div>
<div id="body">body</div>
</div>
</body>
</html>

Centauri
02-12-2008, 05:34 PM
You cannot make a div "take up remaining height" - they don't work that way. The trick with these 100% height layouts is to use minimum height (with height given to IE6 through a star-html hack) and set the background of the 100% height container to make it LOOK like the bottom section goes all the way :* {
margin: 0;
padding: 0;
}
body, html {
height: 100%;
}
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
}
div#canvas {
background-color: #A9A9A9;
min-height: 100%;
}
* html #canvas {
height: 100%;
}
div#header {
background-color: #D3D3D3;
height: 196px;
}
div#body {
}

Lypheus
02-12-2008, 09:46 PM
Ah well, this is exactly what I was worried about too ... ok then, i'll have to re-arrange my gui to deal with this limitation ... that or use absolute positioning and clip based on x,y intersecting with the lower edge of the header and rightmost edge of the left side navigation panel.

Thanks for your help :) !