In JavaScript, the shadowRoot property of Element is a read-only reference to the shadow root hosted by the element (MDN). Use attachShadow() to create one.
Read the notes below before you get or set this property. Prefer feature detection when support is limited.
Syntax
Use el.shadowRoot. Start with this line.
JavaScript
el.shadowRoot
Call
What it does
el.shadowRoot
Primary signature.
Built into the browser. Read or assign el.shadowRoot — no import required.
Library declarations
JavaScript
el.shadowRoot
Parameters
Parameter
Description
attachShadow()
Required. Creates the ShadowRoot
mode: "closed"
Optional. Hides internals from shadowRoot
Host element
Required. Must be allowed to host a shadow tree
Return Value
Return Value
Description
*
The current value of el.shadowRoot.
JavaScript Examples
Example 1 — Open Mode Access
Attach an open shadow root, then read it from the host.
Element.shadowRoot is Baseline Widely available (MDN: across browsers since January 2020). It is not Deprecated, Experimental, or Non-standard.
✓ Baseline Widely available
Element.shadowRoot
Read-only access to an open ShadowRoot. Returns null for closed mode and some built-in UA shadow trees.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
shadowRootBaseline
Bottom line: Safe for modern apps. Prefer open mode when you need host.shadowRoot; keep attachShadow()'s return value for closed roots.
Frequently Asked Questions
It returns the ShadowRoot hosted by the element when the shadow tree was attached in open mode, or null when there is no accessible shadow root (MDN).
No. MDN marks Element.shadowRoot as Baseline Widely available (since January 2020). It is not Deprecated, Experimental, or Non-standard.
When the element has no shadow root, when attachShadow used mode: "closed", or for some built-ins (for example <video>, <audio>) that use closed user-agent shadow roots (MDN).
Call element.attachShadow({ mode: "open" }) or { mode: "closed" }. For open mode, element.shadowRoot then returns the ShadowRoot.
No. It is read-only. Attach a shadow with attachShadow(); do not assign to shadowRoot.
Not through element.shadowRoot — that stays null. Keep the ShadowRoot returned by attachShadow() if you need internal access (MDN).