web api interface
StyleSheetList
The StyleSheetList interface represents a list of CSSStyleSheet objects. An instance of this object can be returned by styleSheets.
It is an array-like object but can’t be iterated over using Array methods. However it can be iterated over in a standard for loop over its indices, or converted to an Array.
[!NOTE] Typically list interfaces like
StyleSheetListwrap aroundArraytypes, so you can useArraymethods on them. This is not the case here for historical reasons. However, you can convertStyleSheetListto anArrayin order to use those methods (see the example below).
Instance properties
lengthRead only- : Returns the number of
CSSStyleSheetobjects in the collection.
- : Returns the number of
Instance methods
item()- : Returns the
CSSStyleSheetobject at the index passed in, ornullif no item exists for that index.
- : Returns the
Examples
Get CSSStyleSheet objects with for loop
for (const styleSheet of document.styleSheets) {
console.log(styleSheet); // A CSSStyleSheet object
}
Get all CSS rules for the document using Array methods
const allCSS = [...document.styleSheets]
.map((styleSheet) => {
try {
return [...styleSheet.cssRules].map((rule) => rule.cssText).join("");
} catch (e) {
console.log(
"Access to stylesheet %s is denied. Ignoring…",
styleSheet.href,
);
return undefined;
}
})
.filter(Boolean)
.join("\n");
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.