tckmn
tckmn

Reputation: 59273

How to get height of the entire available space?

I need to get the height of the entire available space in the web browser, so I can position a div halfway down the screen and stretching it all the way down.

I thought document.body.clientHeight would work, but that returned the amount of space that was being taken up, not total available space.

I also tried making a "wrapper" div around everything:

<div id="wrapper" style="height: 100%; width: 100%;">
    //my other code
</div>

And getting the height of that, but that still returns taken up space.

How can I get the total available space in the web browser with Javascript or jQuery?

Upvotes: 2

Views: 7271

Answers (5)

user3899548
user3899548

Reputation: 41

it is very simple

var max_height=screen.availHeight - (window.outerHight - window.innerHight)

Upvotes: 1

writeToBhuwan
writeToBhuwan

Reputation: 3281

I you mean the space available in parent of the element, You can try this...

var entireHeight=$('#elementID').parent().height();

var siblingHeight=0;

var elementSiblings=$('#elementID').siblings();

$.each(elementSiblings, function() {
    siblingHeight= siblingHeight+$(this).height();
});

Subtracting the values i.e.

(entireHeight-siblingHeight) 

should give you the available space.

Hope it helps.. :) Tell me If it aint working.

Upvotes: 1

SHAKIL DANISH
SHAKIL DANISH

Reputation: 69

you can directly use in javascript

screen.width
screen.height

u can assign like

document.getElementById("parentId").style.width = (screen.width - 100)+"px";
document.getElementById("parentId").style.height = (screen.height-300)+"px"; 

Upvotes: 0

aorlando
aorlando

Reputation: 668

A simple example with jquery

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>
        A2CARE 2.0
    </title>

    <script type="text/javascript" language="Javascript" src="jslib/jquery.js"></script>
    <script type="text/javascript">

        function getheight(){
            var d= document.documentElement;
            var b= document.body;
            var who= d.offsetHeight? d: b ;
            return who.clientHeight;

        }
        $(document).ready(function() {      
                var h = getheight();
                document.getElementById('tryme').style.height= h + 'px';
            });
    </script>
</head>
<body>  
    <div id="tryme" style="border: 2px solid red;">
        try me!
    </div>
</body>

Upvotes: 0

epascarello
epascarello

Reputation: 207501

From the jQuery docs height()

This method is also able to find the height of the window and document.

    $(window).height();   // returns height of browser viewport
    $(document).height(); // returns height of HTML document

Upvotes: 5

Related Questions