4

Im looking for a way, in javascript, to calculate the size of the browser (px) then calculate the size of a <div> taking away 50px from that full screen size:

E.g.

Browser screen size: 800px (Height)
Existing <div> that is always 50px (Height)
Leaves 750px (Height) for the remaining <div> to fill the page.

Then take that 750px and apply it as inline style:

<div style="height: 50px">
<img src="banner.png" />
</div>

<div style="height: x">
This fills the remainder of the page
</div>
1
  • If you want to check other properties (margin, padding and border) that can contribute to a DIV's height, use outerHeight - api.jquery.com/outerHeight Commented Apr 27, 2010 at 9:49

1 Answer 1

5

I assume you mean document height, not browser height. You can do this using something like the following:

$("#div2").height($(document).height() - 50);

// or for more dynamicity
$("#div2").height($(document).height() - $("#div1").height());    

If you did mean browser height and not document height, substitute $(document) with $(window).

Sign up to request clarification or add additional context in comments.

4 Comments

Can you add another element to subtract: $("#div2").height($(document).height() - $("#div1") - $("#div3").height());
@danit: if you want to, sure. Add as many elements as you like. Make sure you're calling the height() method on them, though -- eg. $("#div2").height($(document).height() - $("#div1").height() - $("#div3").height());
Thanks, another question. Possible to resize #div2 when the document is resized? Tried .resize but thats for the window not the document.
@danit, when the window resizes, so should the document. So you would use $(window).resize(function () { /* do calculation here */ }. Apparently, in Firefox the resize event will only fire when you finish resizing. In other browsers it fires as you are dragging the window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.