CSSGroupingRule.insertRule()

The insertRule() method of the CSSGroupingRule interface adds a new CSS rule to a list of CSS rules.

Syntax

cssGroupingRule.insertRule(rule);
cssGroupingRule.insertRule(rule, index);

Parameters

rule

A CSSOMString

indexOptional

An optional index at which to insert the rule; defaults to 0.

Return value

The index of the new rule.

Exceptions

IndexSizeError DOMException

Thrown if index is greater than the number of child CSS rules.

HierarchyRequestError DOMException

Thrown if, due to constraints specified by CSS, the new rule cannot be inserted into the list at the (zero-index) index position given.

InvalidStateError DOMException

Thrown if the new rule is an @namespace at-rule, and the list of child CSS rules contains anything other than @import at-rules and @namespace at-rules.

Examples

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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
insertRule
45
12
20
No
32
No
45
45
20
32
No
5.0

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CSSGroupingRule/insertRule