Create CSS Selector rule with Javascript - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Style

Description

Create CSS Selector rule with Javascript

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 w  w  w  .  j a va 2 s. co m
sheet.insertRule('.should-be-red { color: red; }', 0);

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

Related Tutorials