files/en-us/web/api/cssgroupingrule/insertrule/index.md
{{ APIRef("CSSOM") }}
The insertRule() method of the
{{domxref("CSSGroupingRule")}} interface adds a new CSS rule to a list of CSS rules.
insertRule(rule)
insertRule(rule, index)
rule
index {{optional_inline}}
The index of the new rule.
IndexSizeError {{domxref("DOMException")}}
HierarchyRequestError {{domxref("DOMException")}}
rule cannot be inserted at the specified index due to some CSS constraint.HierarchyRequestError {{domxref("DOMException")}}
rule is a valid statement but not a nested statement.let myRules = document.styleSheets[0].cssRules;
myRules[0].insertRule(
"html {background-color: blue;}",
0,
); /* inserts a rule for the HTML element at position 0 */
{{Specifications}}
{{Compat}}