Set body width,same as clients window

What i’m looking for,is a way to set the width of the body same as the clients window’s,using JAVA.
This code i’ve borrowed from W3S,but its not working in my case –

Read More
function body_width(w)
{

var w = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;

var x = document.body;
x.style.width = w;

}

window.onload = body_width(w);

Also i’ve gave "style="width:;" " to the body

Related posts

5 comments

  1. Have you tried using screen.availWidth and screen.availHeight?

    The screen.availHeight property returns the height of the visitor’s screen, in pixels, minus interface features like the Windows Taskbar.

    The screen.availWidth property returns the width of the visitor’s screen, in pixels, minus interface features like the Windows Taskbar.

    It could be done easily with Viewport-Percentage Lengths:

    The viewport-percentage lengths are relative to the size of the initial containing block. When the height or width of the initial containing block is changed, they are scaled accordingly.

    HTML

    <div>Your Container</div>

    And CSS

    div {
            height:100vh;
        }

    That is all you need.

    Here is JSFiddle example.

  2. This option worked out perfectly.

    <script>
    function body_width(w)
    {
    
    var w = screen.availWidth
    var x = document.body;
    x.style.width = screen.availWidth + "px";
    
    }
    
    window.onload = body_width;
    </script>

    Thanks 2 ivarz_LV

  3. When I tested, using

    body {
     height: 100%;
    }
    

    won’t work. Try using this in style:

    body {
     height: 100vh;
    }
    

    I figured using vh would work better.

Comments are closed.