tags:

views:

149

answers:

5

I'm trying to get the following layout: Link to Image

And this is what I'm doing in code:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
</head>
<body>
<div style="position:absolute; width: 100%; height: 100%" id="container">
     <div id='Header'>Header</div>
     <div id='Content' style='height:100%; background: gray;'>Content</div>
</div>     
</body>
</html>

But this way content gets height of the browser window (parent div), and adds it to first 20 pixels occupied by header, resulting in window scrolling (20px + height of the window == out of the screen). I can use "overflow: hidden" for the content, but I don't want to give the content extra space.

In WPF I could do this by adding a Grid with two rows (Height="Auto" and Height="*"). Maybe I should give up and use tables?

A: 

Would there be any problem just setting the background colour of the body to blue and then letting the content div have a variable height? It would achieve exactly the same effect.

Then you won't even have to have the container div. No point having unnecessary elements.

sixfoottallrabbit
Can't do this. In the content div I can have an iframe, which should be stretched vertically, exactly to the bottom of the container and not more.
Anvaka
A: 

You can change the style for the second div to this:

#Content {
  position: absolute;
  top: 20px;
  bottom: 0px;
  width: 100%;
}

That should keep everythign within the browser frame and not add the 20px.

mishac
A: 

Hi,

I have a fluid height code.Hope this helps.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>DIV layout test</title>

<style type="text/css">
body{
margin: 0;
font-family: tahoma;
font-size: 12px;
color: #645728;
}

#header{
display: block;
width: 100%;
background: #fcf1cb;
}

#middle{
display: block;
width: 100%;
border-top:none;
background: #bce5e3;
}

#footer{
display: block;
width: 100%;
border-top: none;
background: #bce5e3;
}
</style>

<script type="text/javascript">
function getWindowInnerH(){
var innerH = null;
if (typeof(window.innerHeight) == "number"){//MOZILLA, OPERA
innerH = window.innerHeight;
}else if (typeof(document.documentElement.clientHeight) == "number"){//IE 6+
innerH = document.documentElement.clientHeight;
}

return innerH;
}

function arrange(){
var headerE = document.getElementById("header");
var middleE = document.getElementById("middle");
var footerE = document.getElementById("footer");

if (!headerE || !middleE || !footerE){
return;
}

//get window's inner height
var innerH = getWindowInnerH();

//get header's and footer's height
var headerH = headerE.offsetHeight;
var footerH = footerE.offsetHeight;
//calc middle's heigth
var middleH = Math.round(innerH - (headerH + footerH));

//set header's and footer's height
headerE.style.height = headerH + "px";
footerE.style.height = footerH + "px";

//set middle's height
middleE.style.height = middleH + "px";

}

window.onresize = arrange;
</script>

</head>
<body onload="arrange()">
<div id="header">header content<br/><br/><br/><br/><br/></div>
<div id="middle">middle content</div>
<div id="footer">footer content</div>
</body>
</html>

Note: Don't remove the footer div. If you do this technique wont works..

Logesh Paul
A: 

Setting width: 100% , as you know, will be like setting width: <width of the window>.

Instead you can set the Content's edges to be at 0px from the right, left and bottom of the screen.

The CSS of your page will be like this:

body {
    margin: 0px;
    top: 0px;
}
#container {
    color: white;
}
#Header { 
    background-color: grey;
    padding: 6px;
}
#Content {
    position: absolute;
    top: 30px;
    bottom: 0px;
    right: 0px;
    left: 0px;
    margin: 0px;
    padding-top: 6px;
    padding-left: 6px;
    background-color: blue;
}

This way the page has the same look as the image you provided.

Carmine Paolino
Thank you! This is exactly what I needed!
Anvaka
Heh, unfortunately this solution doesn't work in the "browser". (IE 6 of course)
Anvaka
A: 

The easiest way would be to omit the height: property of your content-div and allow it to expand/contract to suit the content. For this to achieve the look of the image you provided, you would need the body of the page to have the same background-property of the content-div. The idea is that where the background of Content finishes the background of body should continue.

body,
Content {background-color: #00f; }

This gets slightly trickier if you use background-images, requiring that both the body and Content are position: absolute, from memory.

David Thomas