Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

ShadowRoot: adoptedStyleSheets property

Baseline2023
Newly available

TheadoptedStyleSheets property of theShadowRoot 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 theCSSStyleSheet() 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 multipleShadowRoot instances, and by the parent document (using theDocument.adoptedStyleSheets property).Changing an adopted stylesheet will affect all the adopting objects.

Stylesheets in theadoptedStyleSheets 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 addedafter the other stylesheets in the shadow DOM (ShadowRoot.styleSheets).

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

Value

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

If the array needs to be modified, use in-place mutations likepush(). TheCSSStyleSheet 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 toadoptedStyleSheets.

Examples

Adopting a stylesheet

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

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

We then create aShadowRoot and pass the sheet object toadoptedStyleSheets 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 DOMshadow.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 usingCSSStyleSheet.insertRule().

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

Append a new stylesheet

New stylesheets can beappended to the document or shadow root by usingadoptedStyleSheets.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

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp