files/en-us/web/api/document/adoptedstylesheets/index.md
{{APIRef("CSSOM")}}
The adoptedStyleSheets property of the {{domxref("Document")}} interface is used for setting an array of constructed stylesheets to be used by the document.
[!NOTE] A constructed stylesheet is a stylesheet created programmatically using the
CSSStyleSheet()constructor (as compared to one created by a user-agent when importing a stylesheet from a script, imported using {{HTMLElement('style')}} and {{CSSXref('@import')}}, or linked to via {{HTMLElement('link')}}).
The same constructed stylesheets can also be shared with one or more {{domxref("ShadowRoot")}} instances using the ShadowRoot.adoptedStyleSheets property.
Changing an adopted stylesheet will affect all the objects that adopt it.
Stylesheets in the property are evaluated along with the document's other stylesheets using the CSS cascade algorithm.
Where the resolution of rules considers stylesheet order, adoptedStyleSheets are assumed to be ordered after those in Document.styleSheets.
Only stylesheets created using the CSSStyleSheet() constructor within the context of the current {{domxref("Document")}} may be adopted.
The value is an array of {{domxref("CSSStyleSheet")}} instances that must have been created using the {{domxref("CSSStyleSheet.CSSStyleSheet()", "CSSStyleSheet()")}} constructor within the context of the same {{domxref("Document")}}.
If the array needs to be modified, use in-place mutations like push(). The {{domxref("CSSStyleSheet")}} instances themselves can also be modified, and these changes will apply wherever the stylesheet is adopted.
In an earlier version of the specification, the array was not modifiable, so the only way to add new stylesheets was to assign a new array to adoptedStyleSheets.
NotAllowedError {{domxref("DOMException")}}
CSSStyleSheet() constructor or was constructed in a different document than the current document, such as one in a frame.The code below shows a stylesheet being constructed, and then {{domxref("CSSStyleSheet.replaceSync()")}} is called to add a rule to the sheet.
The stylesheet is then added to an array and assigned to the adoptedStyleSheets property.
// Create an empty "constructed" stylesheet
const sheet = new CSSStyleSheet();
// Apply a rule to the sheet
sheet.replaceSync("a { color: red; }");
// Apply the stylesheet to a document
document.adoptedStyleSheets.push(sheet);
We can append a new rule to the stylesheet using {{domxref("CSSStyleSheet.insertRule()")}}.
sheet.insertRule("* { background-color: blue; }");
// The document will now have blue background.
We can share a stylesheet to a shadow root in a similar way.
// Create an element in the document and then create a shadow root:
const node = document.createElement("div");
const shadow = node.attachShadow({ mode: "open" });
// Adopt the same sheet into the shadow DOM
shadow.adoptedStyleSheets = [sheet];
{{Specifications}}
{{Compat}}
CSSStyleSheet() constructor