I’ve been looking at a few different things I’d like to using JavaScript to tweak styles globally. I’d like to do this by changing the CSS rule that dictates the element’s style (akin to doing this through the Inspector in Webkit), but after coming to https://developer.mozilla.org/en/DOM/CSSStyleRule I now don’t know if this is even possible:
styleReturns the
CSSStyleDeclarationobject for the rule. Read only.
So, is there no way to change higher-level styles in JavaScript?
To modify your existing styles, either find the stylesheet in
document.styleSheetsor from the the.sheetproperty of the<style>or<link>element you want to modify. Then modify the properties in whatever rule they’re located in (see https://developer.mozilla.org/en/DOM/CSSRuleList). I’d advice against using the CSSOM to modify properties, as browser support for modifying CSS properties through the CSSOM is pitiful (no browsers whatsoever support it). Instead, just set a string value.If all you want to do is insert a new rule, just get a stylesheet from the method above, or
document.documentElement.appendChild(document.createElementNS("http://www.w3.org/1999/xhtml","style")).sheet. Then useinsertRuleto add your rule.