Change background color if width is bigger - Javascript CSS Style Property

Javascript examples for CSS Style Property:backgroundColor

Description

Change background color if width is bigger

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <style type="text/css">

#mydiv {//from w ww  .j a v  a  2s .c  o m
   width: 200px;
   height: 100px;
   background-color: red;
}

      </style> 
   </head> 
   <body> 
      <div id="mydiv"></div> 
      <script>
  var mydiv = document.getElementById("mydiv");
  var clientRect = mydiv.getBoundingClientRect()
  var curr_width = clientRect.width || (clientRect.right - clientRect.left);
  if (curr_width > 100) {
      mydiv.style.backgroundColor = "blue";
  }else{
      mydiv.style.backgroundColor = "red";
  }

      </script>  
   </body>
</html>

Related Tutorials