Cookies improve the way our website works, by using this website you are agreeing to our use of cookies. For more information see our privacy policy.
OK
The rules collection does not contain the at-rules only style rules, while the cssRules collection contains both style rules and at-rules.
The cssRules collection is supported in all browsers except in Internet Explorer before version 9.
For further details, please see the page for the rules and cssRules collections.
Retrieves an integer value that identifies the type of the CSS rule represented by the current CSSRule object.
Example HTML code 1:
This example shows how to change a style rule in an embedded style sheet:
<head><styleid="myStyle">.red {
color: red;
}
</style><scripttype="text/javascript">function ChangeRedColor () {
var styleTag = document.getElementById ("myStyle");
// the style sheet in the style tag
var sheet = styleTag.sheet ? styleTag.sheet : styleTag.styleSheet;
// the first rule in the style sheet
var rules = sheet.cssRules ? sheet.cssRules : sheet.rules;
var firstRule = rules[0];
// change the color
firstRule.style.color = "#00ff00";
}
</script></head><body><buttononclick="ChangeRedColor ()">Change the color of the '.red' style rule!</button><br/><br/><divclass="red">red division</div><div>non-red division</div><spanclass="red">red span</span></body>
This example shows how to change a style rule in an external style sheet:
Code
red.css
<head><linkid="myLink"rel="stylesheet"type="text/css"href="red.css"/><scripttype="text/javascript">function ChangeRedColor () {
var linkTag = document.getElementById ("myLink");
// the imported style sheet
var importedSheet = linkTag.sheet ? linkTag.sheet : linkTag.styleSheet;
// the first rule in the style sheet
var rules = importedSheet.cssRules ? importedSheet.cssRules : importedSheet.rules;
var firstRule = rules[0];
// change the color
firstRule.style.color = "#00ff00";
}
</script></head><body><buttononclick="ChangeRedColor ()">Change the color of the '.red' style rule!</button><br/><br/><divclass="red">red division</div><div>non-red division</div><spanclass="red">red span</span></body>