HTML Element Style How to - Change the size of a div according to window size








Question

We would like to know how to change the size of a div according to window size.

Answer


<html>
<head>
<style type='text/css'>
div {<!--from  w w  w . j a  va 2  s  .c o m-->
   border: 1px solid #000000;
} 
</style>
<script type='text/javascript'>
function expandToWindow(element) {
     var margin = 10; 

     if (element.style.height < window.innerHeight) { 
        element.style.height = window.innerHeight - (2 * margin) 
     }
}
</script>
</head>
<body onload='expandToWindow(document.getElementById("content"));'>
    <div id='content'>Hello World</div>
</body>
</html>

The code above is rendered as follows: