views:

6261

answers:

9

In a web application I'm working on, I have a page that contains a DIV that has an auto-width depending on the width of the browser window.

However, I need to be able to have an auto-height for the object. The DIV starts about 300px from the top screen, and it's height should make it stretch to the bottom of the browser screen. I have a max height for the container DIV, so there would have to be minimum-height for the div. But I believe I can just restrict that in CSS, and use Javascript to handle the resizing of the DIV.

My javascript isn't nearly as good as it should be. Is there an easy script I could write that would do this for me?

Edit: The DIV houses a control that does it's own overflow handling (implements its own scroll bar).

+1  A: 

What should happen in the case of overflow? If you want it to just get to the bottom of the window, use absolute positioning:

div {
  position:absolute;
  top:300px;
  bottom:0px;
  left:30px;
  right:30px;
}

This will put the div 30 px in from each side, 300 px from the top of the screen, and flush with the bottom. Add an "overflow:auto;" to handle cases where the content is larger than the div.


Edit: @Whoever marked this down, an explanation would be nice... Is something wrong with the answer?

Chris Marasti-Georg
This seems the simplest to me.
Tom B
A: 

The DIV houses a control that does it's own overflow handling.

1kevgriff
A: 

If I understand what you're asking, this should do the trick:

// the more standards compliant browsers (mozilla/netscape/opera/IE7) use 
// window.innerWidth and window.innerHeight

var windowHeight;

if (typeof window.innerWidth != 'undefined')
{
    windowHeight = window.innerHeight;
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first 
// line in the document)
else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth != 'undefined' 
        && document.documentElement.clientWidth != 0)
{
    windowHeight = document.documentElement.clientHeight;
}
// older versions of IE
else
{
    windowHeight = document.getElementsByTagName('body')[0].clientHeight;
}

document.getElementById("yourDiv").height = windowHeight - 300 + "px";
17 of 26
A: 

document.getElementById('myDiv').style.height = 500;

This is the very basic JS code required to adjust the height of your object dynamically. I just did this very thing where I had some auto height property, but when I add some content via XMLHttpRequest I needed to resize my parent div and this offsetheight property did the trick in IE6/7 and FF3

Toran Billups
A: 

@Toran Explanation please? What does that do?

Chris Marasti-Georg
A: 

@17 of 26 I used to have a solution like this - I found that it ran into weird issues with ie 6.0 sometimes being off by a few pixels, which really made the layout look odd.

Chris Marasti-Georg
A: 

@17 of 26 I used to have a solution like this - I found that it ran into weird issues with ie 6.0 sometimes being off by a few pixels, which really made the layout look odd.

Yeah, in my solution I just wound up shrinking the height by an arbitrary number of pixels for IE.

I could not find any other way to dynamically size a div though. A lot of my layout uses tables since they handle dynamically sized content automatically.

Cross-browser web design just plain sucks :P.

17 of 26
+5  A: 

Try this simple, specific function:

function resizeElementHeight(element) {
  var height = 0;
  var body = window.document.body;
  if (window.innerHeight) {
      height = window.innerHeight;
  } else if (body.parentElement.clientHeight) {
      height = body.parentElement.clientHeight;
  } else if (body && body.clientHeight) {
      height = body.clientHeight;
  }
  element.style.height = ((height - element.offsetTop) + "px");
}

It does not depend on the current distance from the top of the body being specified (in case your 300px changes).


EDIT: By the way, you would want to call this on that div every time the user changed the browser's size, so you would need to wire up the event handler for that, of course.

Jason Bunting
A: 

With minor corrections:

function rearrange()
{
var windowHeight;

if (typeof window.innerWidth != 'undefined')
{
 windowHeight = window.innerHeight;
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first
// line in the document)
else if (typeof document.documentElement != 'undefined'
  && typeof document.documentElement.clientWidth != 'undefined'
  && document.documentElement.clientWidth != 0)
{
 windowHeight = document.documentElement.clientHeight;
}
// older versions of IE
else
{
 windowHeight = document.getElementsByTagName('body')[0].clientHeight;
}

document.getElementById("foobar").style.height = (windowHeight - document.getElementById("foobar").offsetTop  - 6)+ "px";
}
Shanti