ShadowRoot: adoptedStyleSheets property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.

The adoptedStyleSheets property of the ShadowRoot interface sets an array of constructed stylesheets to be used by the shadow DOM subtree.

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 <style> and @import, or linked to via <link>).

The same constructed stylesheet can be adopted by multiple ShadowRoot instances, and by the parent document (using the Document.adoptedStyleSheets property). Changing an adopted stylesheet will affect all the adopting objects.

Stylesheets in the adoptedStyleSheets property are considered along with the shadow DOM's other stylesheets. For the purpose of determining the final computed CSS of any element, they are considered to have been added after the other stylesheets in the shadow DOM (ShadowRoot.styleSheets).

Only stylesheets created using the CSSStyleSheet() constructor, and from within the same parent Document as the shadow root, may be adopted.

Value

The value is an array of CSSStyleSheet instances that must have been created using the CSSStyleSheet() constructor within the context of the shadow root's parent Document.

If the array needs to be modified, use in-place mutations like push(). The 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.

Examples

Adopting a stylesheet

The code below first shows a stylesheet being constructed, and then CSSStyleSheet.replaceSync() is called to add a rule to the sheet.

js
// Create an empty "constructed" stylesheet
const sheet = new CSSStyleSheet();
// Apply a rule to the sheet
sheet.replaceSync("a { color: red; }");

We then create a ShadowRoot and pass the sheet object to adoptedStyleSheets inside an array.

js
// 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 sheet into the shadow DOM
shadow.adoptedStyleSheets = [sheet];

We can still modify the stylesheets after they have been added to the array. Below we append a new rule to the same sheet using CSSStyleSheet.insertRule().

js
sheet.insertRule("* { background-color: blue; }");
// The document will now have blue background.

Append a new stylesheet

New stylesheets can be appended to the document or shadow root by using adoptedStyleSheets.push():

js
const extraSheet = new CSSStyleSheet();
extraSheet.replaceSync("p { color: green; }");

// Concat the new sheet.
shadow.adoptedStyleSheets.push(extraSheet);

Specifications

Specification
CSS Object Model (CSSOM)
# dom-documentorshadowroot-adoptedstylesheets

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
adoptedStyleSheets
Chrome – Full support
Chrome 73 (Release date: 2019-03-12)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 101 (Release date: 2022-05-31)
footnote Full support
Opera – Full support
Opera 60 (Release date: 2019-04-09)
footnote Full support
Safari – Full support
Safari 16.4 (Release date: 2023-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 73 (Release date: 2019-03-12)
footnote Full support
Firefox for Android – Full support
Firefox for Android 101 (Release date: 2022-05-31)
footnote Full support
Opera Android – Full support
Opera Android 50 (Release date: 2019-02-18)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16.4 (Release date: 2023-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 11 (Release date: 2019-12-05)
footnote Full support
WebView Android – Full support
WebView Android 73 (Release date: 2019-03-12)
footnote Full support
WebView on iOS – Full support
WebView on iOS 16.4 (Release date: 2023-03-27)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also