You CAN affect the document’s stylesheets in JS… a couple of ways, actually.
To manipulate global CSS (non-element CSS) using JavaScript, you can create a <style>
tag like any other HTML element. From there you’ve got a couple options for applying the styling.
-
Set innerHTML
var style = document.createElement('style'); style.innerHTML = ` #app { background-color: blueviolet; } `; document.head.appendChild(style);
-
Or use a method of the CSSStyleSheet class.
var style = document.createElement('style'); document.head.appendChild(style); style.sheet.insertRule('#app {background-color: darkseagreen}'); //STYLESHEET MANIPULATION Option 2
Source
CSSStyleSheet - Web APIs | MDN
Set CSS styles with javascript