Jump to content

Set website height according to screen height


charltondurie

Recommended Posts

I have a website that I want to have it's height flexible. So depending on the screen the user is viewing it on, the site will render to that height.

 

I was trying to use the following javascript in my header tag to set the height of my 'main' id div but with no luck. Any clue's?

<script type="text/javascript" language="javascript">
document.getElementById("main").style.height = screen.height;
</script>

Link to comment
Share on other sites

Code like that in the head of the document will never be run. You need to assign the code to an event:

 

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

 

Problem here is that this code won't get executed until the page has finished loading, meaning the effect will be very noticeable as the DIV suddenly changes height. I'd move it to the bottom of your body, where the code will be executed instantly and you don't need to assign it to an event:

 

[...]
    <script type="text/javascript">
        document.getElementById("main").style.height = screen.height;
    </script>
</body>
</html>

 

Although I should mention, not everybody has JS enabled and this kind of reliance on it for the design isn't the best solution. Perhaps if you post a link to your site we can help you implement the same effect using CSS?

Link to comment
Share on other sites

I am now using it to center the page as can be seen on the temp site here: http://www.tenpoint.com.au/wfhs/ I was unable to get it all to work with css do to having to use absolute positioning to achieve my fluid layout and scrollable center part of the page.

 

Any pointers to better achieve this would be greatly appreciated.

 

I ended u using the following JS:

<script type="text/javascript" language="javascript">
var left = (document.documentElement.clientWidth - '800') / '2';
document.getElementById('main').style.left = left + 'px';

window.onresize = function () {
	var left = (document.documentElement.clientWidth - '800') / '2';
	document.getElementById('main').style.left = left + 'px';
}
</script>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.