Created
June 26, 2020 20:20
-
-
Save TunedMystic/f9fdd6308b59605662b10d3ea8f02612 to your computer and use it in GitHub Desktop.
Add CSS styles to a webpage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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