Dynamically inject a CSS code into the page [JavaScript]

Mozilla created a function that permits to inject some CSS into the document: addStylesheetRules()

But if you use jQuery you may want to use this function:

function injectCSS(rule) {
  $("head").append('<style>' + rule + '</style>')
}

// example
injectCSS("div { padding:5px }")

Leave a Reply

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

*