Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing div height according screen resolution javascript

Here is the problem. I have

<div id="main"></div>

I want to check user resolution and change his height according user resolution, using javascript?

Javascript:

window.onload = function() {
    var height = screen.height
    document.getElementById(main).style.height = height;
};

I even try this:

window.onload = function() { 
    var height = screen.height 
    var ele = document.getElementById(main);
    if(ele.style.height == "auto") 
    {
            ele.style.height = height;
    }
    else {
        ele.style.height = height;
    }
}; 
like image 938
Tanja Pakovic Avatar asked Jun 25 '26 03:06

Tanja Pakovic


1 Answers

If you want set the Screen Height:

var mainNode = document.getElementById("main");
mainNode.style.height = screen.height + "px";

Screen Avail Height

Screen Height

DOMElement Client Height

Screen height is different from client height (document.documentElement.clientHeight).

like image 74
JNo Avatar answered Jun 28 '26 10:06

JNo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!