JavaScript: insertRule

Published on Author craigzearfossLeave a comment
/*
Inserts a CSS style rule.

Usage:
    sheet.insertRule("header { float: left; opacity: 0.8; }", 1);false

reference: http://davidwalsh.name/essential-javascript-functions
*/

var sheet = (function() {
   // Create the <style> tag
   var style = document.createElement('style');

   // Add a media (and/or media query) here if you'd like!
   // style.setAttribute('media', 'screen')
   // style.setAttribute('media', 'only screen and (max-width : 1024px)')

   // WebKit hack :(
   style.appendChild(document.createTextNode(''));

   // Add the <style> element to the page
   document.head.appendChild(style);

   return style.sheet;
})();

Leave a Reply

Your email address will not be published. Required fields are marked *