Skip to content

Shadow DOM

Shadow DOM is a browser API that lets you attach a private, scoped DOM tree to any HTML element. That private tree is called the shadow tree, and the element it is attached to is called the host element. The rest of the page — the normal document tree — is called the light DOM.

The shadow tree is completely separate from the light DOM. Styles written inside it cannot leak out to the page, and page styles cannot accidentally style elements inside it. This is what “encapsulation” means in the context of web components.

TermMeaning
Host elementThe element that owns the shadow root (e.g., <my-card>)
Shadow rootThe root node of the shadow tree, returned by attachShadow()
Shadow treeAll the nodes inside the shadow root
Light DOMThe main document tree — everything outside the shadow root
Encapsulated stylesCSS declared inside the shadow root that is scoped to the shadow tree only
flowchart LR
  LD["Light DOM"]
  HE["Host Element"]
  SR["Shadow Root"]
  ST["Shadow Tree"]
  IS["Internal <style>"]
  LD --> HE
  HE --> SR
  SR --> ST
  SR --> IS
Shadow DOM structure
LessonTopic
1Shadow DOM — overview and structure (this page)
2Attaching a Shadow Root — attachShadow, shadowRoot.innerHTML
3Style Encapsulation — shadow boundary, scoped CSS
4Open vs Closed Mode — mode: 'open' vs mode: 'closed'
5Host Element and Shadow Boundary — :host, :host(selector), event retargeting

The element below attaches a shadow root and renders a styled greeting entirely inside the shadow tree. The blue color comes from a <style> tag placed inside the shadow root — not from the page.

What is the element that owns a shadow root called?
Which term describes the normal document tree that exists outside the shadow root?
What is the main benefit of placing a `<style>` tag inside a shadow root rather than in the document head?