Call insertRule() function on style element sheet property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Style

Description

Call insertRule() function on style element sheet property

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width"> 
   </head> 
   <body> 
      <div class="should-be-red">
         I should be red
      </div> 
      <script>
var styleElement = document.createElement('style');
document.head.appendChild(styleElement);
var sheet = styleElement.sheet;/*from   ww w . j  a  va2s.c o  m*/
sheet.insertRule('.should-be-red { color: red; }', 0);

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

Related Tutorials