jQuery Style How to - Set div width to screen.width








Question

We would like to know how to set div width to screen.width.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.3.js'></script>
<style type='text/css'>
#mainBody {<!--   ww w . j  a v  a  2  s. com-->
  background: #f0eded;
  height: 98px;
  margin-top: 5px;
  width: 100px;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    $("#mainBody").css('width', screen.width);
});
</script>
</head>
<body>
  <div id="mainBody"></div>
</body>
</html>

The code above is rendered as follows: