Element.shadowRoot is a read-only instance property that returns the open ShadowRoot hosted by an element, or null when no accessible shadow tree exists. Learn open vs closed mode, how it pairs with attachShadow(), and how to query inside a shadow tree—with five examples and try-it labs.
01
Kind
Instance property
02
Access
Read-only
03
Type
ShadowRoot or null
04
Open mode
Accessible via property
05
Closed mode
Returns null
06
Status
Baseline widely
Fundamentals
Introduction
Shadow DOM lets an element host a private DOM tree. That tree is rooted at a ShadowRoot. After you attach an open shadow root with attachShadow(), you can reach it later with element.shadowRoot.
If the shadow root was attached as closed, shadowRoot stays null. That is the main beginner rule for this property.
Prefer mode: "open" unless you have a strong reason for closed encapsulation. Open mode is easier to debug and style from outside.
Concept
Understanding the Property
MDN: the read-only shadowRoot property represents the shadow root hosted by the element. Use Element.attachShadow() to add a shadow root to an existing element.
Read-only — create shadow trees with attachShadow(), not assignment.
ShadowRoot or null — null means no accessible shadow root.
Open mode only — closed roots do not appear on this property.
Built-ins may be closed — some browser controls keep shadowRoot null.
Foundation
📝 Syntax
JavaScript
element.shadowRoot
Value
A ShadowRoot object instance, or null if there is no open shadow root.
Item
Detail
Type
ShadowRoot or null
Access
Read-only property
Created by
attachShadow({ mode })
Open
shadowRoot returns the tree
Closed
shadowRoot is null
⚠️
Null safety
Always check for null before calling methods like querySelector() on element.shadowRoot.
Pattern
📋 Open vs Closed Pattern
MDN’s core idea: open roots are readable through shadowRoot; closed roots are not.
Assume closed roots are still readable via the property
Skip null checks in outside tooling code
Expect built-in controls like some form widgets to expose their UA shadow trees
Overuse closed mode without a clear encapsulation need
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about shadowRoot
Read-only access to open Shadow DOM trees, or null when closed/missing.
5
Core concepts
📄01
Get only
on Element
Kind
📝02
ShadowRoot|null
open access
Type
🔍03
Closed = null
key rule
Mode
✅04
Baseline
widely available
Status
🎯05
Prefer open
for most apps
Tip
❓ 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.
No. MDN marks Element.shadowRoot as Baseline Widely available. It is not Deprecated, Experimental, or Non-standard.
It returns null when the element has no shadow root, when the shadow root was attached with mode: "closed", or for some built-in elements that use closed user-agent shadow roots.
Call element.attachShadow({ mode: "open" }) or { mode: "closed" }. For open mode, element.shadowRoot then gives you the ShadowRoot.
No. It is read-only. You attach a shadow root with attachShadow(); you do not assign to shadowRoot.
Not through element.shadowRoot — that stays null. Keep the ShadowRoot returned by attachShadow() if you need internal access.
Did you know?
Some built-in elements, such as <input> and <textarea>, use closed user-agent shadow roots, so their shadowRoot property stays null even though a shadow tree exists internally.