Example 1 — Count Stylesheets
Use length to see how many sheets the document exposes.
const count = document.styleSheets.length;
console.log("Stylesheet count:", count); How It Works
Includes both external linked CSS and inline <style> blocks.

Document.styleSheets is a read-only instance property that returns every CSS stylesheet linked or embedded in the document. Learn how to count sheets, find one by title, read cssRules, compare with adoptedStyleSheets, and five examples with try-it labs.
Read-only property
StyleSheetList
CSSStyleSheet
link + style
cssRules
Baseline widely
Every styled page loads CSS from <link rel="stylesheet"> tags, inline <style> blocks, and sometimes HTTP Link headers. When JavaScript needs to inspect those stylesheets programmatically, document.styleSheets is the standard entry point.
MDN: the styleSheets read-only property returns a StyleSheetList of CSSStyleSheet objects, for stylesheets explicitly linked into or embedded in a document.
styleSheets is a CSS Object Model (CSSOM) list. It complements DOM collections like links (which returns <link> elements) by exposing live stylesheet objects with cssRules.
Related Document tutorials: adoptedStyleSheets, links, scripts, Document constructor.
Document.styleSheetsA read-only instance property from the CSS Object Model.
StyleSheetList of CSSStyleSheet items (MDN).<style> blocks.length, index, or for...of loop.document.styleSheets A StyleSheetList of CSSStyleSheet objects (MDN).
function getStyleSheet(uniqueTitle) {
for (const sheet of document.styleSheets) {
if (sheet.title === uniqueTitle) {
return sheet;
}
}
} | Goal | Code / note |
|---|---|
| Get list | document.styleSheets |
| Count sheets | document.styleSheets.length |
| First sheet | document.styleSheets[0] |
| External URL | sheet.href (null if inline) |
| Find by title | Loop and match sheet.title (MDN) |
| Read rules | sheet.cssRules (same-origin only) |
Four facts about document.styleSheets.
StyleSheetListRead-only
CSSStyleSheetCSSOM
header+DOMMDN
BaselineSince 2015
styleSheets vs adoptedStyleSheetsstyleSheets | adoptedStyleSheets | |
|---|---|---|
| Source | Linked / embedded in markup | Constructable sheets from JS |
| Mutable array | Read-only list | Yes (assign array) |
| Typical use | Inspect page CSS | Inject dynamic CSS at runtime |
| MDN status | Baseline Widely available | Baseline (constructable API) |
Examples follow MDN Document: styleSheets. Each includes a try-it lab you can run in the browser.
Count and list stylesheets on a page.
Use length to see how many sheets the document exposes.
const count = document.styleSheets.length;
console.log("Stylesheet count:", count); Includes both external linked CSS and inline <style> blocks.
Loop the list and show each sheet’s href.
for (const sheet of document.styleSheets) {
console.log(sheet.href || "inline");
} Inline sheets have href === null; external sheets return the full URL string.
Find sheets by title and read selector names.
MDN helper to locate a sheet with a matching title attribute.
function getStyleSheet(uniqueTitle) {
for (const sheet of document.styleSheets) {
if (sheet.title === uniqueTitle) {
return sheet;
}
}
}
const sheet = getStyleSheet("Main");
console.log(sheet ? "Found Main" : "Not found"); The title on <link> or <style> becomes sheet.title.
Print each rule’s selectorText from accessible sheets.
for (const styleSheet of document.styleSheets) {
try {
for (const rule of styleSheet.cssRules) {
console.log(rule.selectorText);
}
} catch (e) {
console.log("(cross-origin sheet — cssRules blocked)");
}
} MDN demo output for body, p, and #lumpy rules. Cross-origin sheets need try/catch.
adoptedStyleSheetsDocument sheets vs constructable sheets attached via JavaScript.
console.log({
styleSheets: document.styleSheets.length,
adoptedStyleSheets: document.adoptedStyleSheets.length
}); styleSheets reflects markup; adoptedStyleSheets holds JS-constructed sheets you assign.
title.href URLs.<link> to live sheets.document.styleSheets WorksFrom <link>, <style>, or Link headers.
Each sheet becomes a CSSStyleSheet with rules.
Ordered list: header sheets first, then DOM tree order (MDN).
Use href, title, and cssRules for inspection tools.
cssRules access (SecurityError) — use try/catch.adoptedStyleSheets for JS-injected sheets.Document.styleSheets is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
Read-only StyleSheetList — linked and embedded CSSStyleSheet objects with cssRules.
Bottom line: Use document.styleSheets to inspect page CSS. Wrap cssRules access in try/catch for cross-origin sheets; pair with adoptedStyleSheets for constructable CSS.
Document.styleSheets is the standard CSSOM list of every stylesheet linked or embedded in a document. Count sheets, read href and title, and inspect cssRules on same-origin CSS. For constructable sheets, use adoptedStyleSheets.
Continue with timeline, adoptedStyleSheets, links, or the JavaScript hub.
document.styleSheets to inventory page CSScssRules reads in try/catchfor...of for claritysheet.href to separate inline vs externaladoptedStyleSheets for dynamic JS CSScssRulesstyleSheets with links DOM listdocument.styleSheetsCSSOM list of linked and embedded stylesheets.
StyleSheetList
APICSSStyleSheet
TypecssRules
CSSOMURL|null
MetaBaseline
2015+MDN states the returned list is ordered with Link-header stylesheets first (sorted in header order), then stylesheets from the DOM (sorted in tree order). That is why programmatic order may differ from visual source order in DevTools.
Learn the document’s default DocumentTimeline for Web Animations.
6 people found this page helpful