Skip to content

Instantly share code, notes, and snippets.

@TunedMystic
Created June 26, 2020 20:20
Show Gist options
  • Save TunedMystic/f9fdd6308b59605662b10d3ea8f02612 to your computer and use it in GitHub Desktop.
Save TunedMystic/f9fdd6308b59605662b10d3ea8f02612 to your computer and use it in GitHub Desktop.
Add CSS styles to a webpage.
function addCss(rule) {
// https://stackoverflow.com/a/38063486
let css = document.createElement('style');
css.type = 'text/css';
if (css.styleSheet) {
css.styleSheet.cssText = rule; // Support for IE
}
else {
css.appendChild(document.createTextNode(rule)); // Support for the rest
}
document.getElementsByTagName("head")[0].appendChild(css);
}
// How to use.
var rules = '#wrap, #about {background: #bcd6f7;} #wrap .lines div {color: #94a9c3;}';
addCss(rules);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment