Skip to main content

Shadow DOM vs DOM

1. Definitions

TypeWhat it is
Regular (Light) DOMThis is the "main" tree of an HTML document - everything you write in HTML and see in DevTools.
Shadow DOMThis is a nested, isolated DOM tree inside an element (a component), which has its own styles, markup, and behavior, and does not affect the outer document.

In other words:

Light DOM is "the surface of the site", Shadow DOM is "the component's insides", hidden from the rest of the world.


2. An example of the difference

Regular DOM

javascript
<div class="card"> <h2>Header</h2> <button>Click</button> </div>

Everything is visible in the main DOM, you can grab it via document.querySelector('button'). Global styles affect everything (button { color: red; } will make the button red).


Shadow DOM

javascript
const card = document.querySelector('.card'); const shadow = card.attachShadow({ mode: 'open' }); shadow.innerHTML = ` <style> button { color: blue; } </style> <button>Inner</button> `;

Now card has an internal Shadow DOM that:

  • is isolated from outer styles,
  • is not visible in the main DOM (if mode: 'closed'),
  • manages its own elements.

Global styles cannot repaint the button from blue → red, because it lives inside its own Shadow tree.


3. Key differences

CharacteristicRegular DOM (Light)Shadow DOM
Where it livesIn the document treeInside a "host" element
Visible in DevToolsYesYes (but hidden by default, can be enabled with "Show user agent shadow DOM")
StylingSubject to global CSSIsolated from global styles
IsolationNone - styles and JS affect each otherFull - an isolated context
Used inAny HTMLWeb Components, built-in elements (<video>, <input>, etc.)
InteractionDirect, via document.querySelector()Only via shadowRoot.querySelector()
SlotsNoneYes (<slot> for inserting content from the Light DOM)

4. Visually

javascript
<body> <my-card> ← host element #shadow-root (shadow DOM) <style>...</style> <button>Click</button> #light-dom (regular DOM) <p slot="content">...</p> </my-card> </body>

my-card is the host element #shadow-root is its internal Shadow DOM Outer elements can pass content inside via <slot>.


5. An example of the interaction

The Light DOM only sees the "shell":

javascript
console.log(document.querySelector('my-card').innerHTML); // Empty, because the insides are hidden inside the Shadow DOM

The Shadow DOM is accessible only through a reference:

javascript
const shadow = document.querySelector('my-card').shadowRoot; console.log(shadow.querySelector('button')); // <button>Click</button>

6. Applications of Shadow DOM

It's used when you need to:

  • Encapsulate a component (web components, UI libraries),
  • Isolate styles (so global CSS does not break the appearance),
  • Create a reusable element (for example, <custom-slider>),
  • Make a component self-contained, like <video> or <select>.

An example from real life

Regular DOM:

You had to redefine button styles across the whole site, and suddenly the buttons in a component "broke" - because the styles are global.

Shadow DOM:

A component with its own styles is isolated, and no global button { color: red } will affect it.


Summary

CriterionRegular DOMShadow DOM
VisibilityAccessible to all codeHidden and isolated
StylesSharedIsolated
UsageEveryday HTMLComponents and built-in elements
InteractionVia documentVia shadowRoot
SecurityNo isolationHas encapsulation

Remember it simply:

  • The regular DOM is shared across the whole page.
  • The Shadow DOM is a separate "mini world" inside an element, with its own styles, logic, and DOM structure.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.