--- title: CSSStyleSheet slug: Web/API/CSSStyleSheet tags: - API - CSSOM - NeedsTranslation - Reference - TopicStub translation_of: Web/API/CSSStyleSheet ---
The CSSStyleSheet
interface represents a single CSS style sheet. It inherits properties and methods from its parent, {{domxref("StyleSheet")}}.
A style sheet consists of {{domxref("CSSRule", "rules", "", 1)}}, such as {{domxref("CSSStyleRule", "style rules", "", 1)}} ("h1,h2 { font-size: 16pt }"
), various at-rules (@import
, @media
, ...), etc. This interface lets you inspect and modify the list of rules in the stylesheet.
See the {{anch("Notes")}} section for the various ways a CSSStyleSheet object can be obtained.
Inherits properties from its parent, {{domxref("StyleSheet")}}.
styleSheet.cssRules[i] // where i = 0..cssRules.length-1
To add or remove items in cssRules
, use the CSSStyleSheet
's deleteRule()
and insertRule()
methods, described below.ownerRule
property will return that {{domxref("CSSImportRule")}}, otherwise it returns null
.Inherits methods from its parent, {{domxref("Stylesheet")}}.
In some browsers, if a stylesheet is loaded from a different domain, calling cssRules
results in SecurityError
.
A stylesheet is associated with at most one {{domxref("Document")}}, which it applies to (unless {{domxref("StyleSheet.disabled", "disabled", "", 1)}}). A list of CSSStyleSheet
objects for a given document can be obtained using the {{domxref("document.styleSheets")}} property. A specific style sheet can also be accessed from its owner object (Node
or CSSImportRule
), if any.
A CSSStyleSheet
object is created and inserted into the document's styleSheets
list automatically by the browser, when a style sheet is loaded for a document. As the {{domxref("document.styleSheets")}} list cannot be modified directly, there's no useful way to create a new CSSStyleSheet
object manually (although Constructable Stylesheet Objects is coming to the web platform soon and is already supported in Blink). To create a new stylesheet, insert a {{HTMLElement("style")}} or {{HTMLElement("link")}} element into the document.
A (possibly incomplete) list of ways a style sheet can be associated with a document follows:
Reason for the style sheet to be associated with the document | Appears in document. list |
Getting the owner element/rule given the style sheet object | The interface for the owner object | Getting the CSSStyleSheet object from the owner |
---|---|---|---|---|
{{HTMLElement("style")}} and {{HTMLElement("link")}} elements in the document | Yes | {{domxref("StyleSheet.ownerNode", ".ownerNode")}} | {{domxref("HTMLLinkElement")}}, {{domxref("HTMLStyleElement")}}, or {{domxref("SVGStyleElement")}} |
{{domxref("LinkStyle.sheet", ".sheet")}} |
CSS {{cssxref("@import")}} rule in other style sheets applied to the document | Yes | {{domxref("CSSStyleSheet.ownerRule", ".ownerRule")}} | {{domxref("CSSImportRule")}} | {{domxref("CSSImportRule.styleSheet", ".styleSheet")}} |
<?xml-stylesheet ?> processing instruction in the (non-HTML) document |
Yes | {{domxref("StyleSheet.ownerNode", ".ownerNode")}} | {{domxref("ProcessingInstruction")}} | {{domxref("LinkStyle.sheet", ".sheet")}} |
HTTP Link Header | Yes | N/A | N/A | N/A |
User agent (default) style sheets | No | N/A | N/A | N/A |
Specification | Status | Comment |
---|---|---|
{{SpecName("CSSOM", "#cssstylesheet", 'CSSStyleSheet')}} | {{Spec2("CSSOM")}} | |
{{SpecName("DOM2 Style", "css.html#CSS-CSSStyleSheet", "CSSStyleSheet")}} | {{Spec2("DOM2 Style")}} | Initial definition |
{{Compat("api.CSSStyleSheet")}}