CSSGroupingRule: insertRule() method
The insertRule() method of the
CSSGroupingRule interface adds a new CSS rule to a list of CSS rules.
Syntax
js
insertRule(rule)
insertRule(rule, index)
Parameters
Return value
The index of the new rule.
Exceptions
IndexSizeErrorDOMException-
Thrown if index is greater than the number of child CSS rules.
HierarchyRequestErrorDOMException-
Thrown if, due to constraints specified by CSS, the new rule cannot be inserted into the list at the (zero-index) index position given.
InvalidStateErrorDOMException-
Thrown if the new rule is an
@namespaceat-rule, and the list of child CSS rules contains anything other than@importat-rules and@namespaceat-rules.
Examples
js
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
| Specification |
|---|
| CSS Object Model (CSSOM) # dom-cssgroupingrule-insertrule |
Browser compatibility
| desktop | mobile | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
insertRule | |||||||||||