What is the difference between shadow DOM and the virtual DOM?
Shadow DOM and the virtual DOM sound alike but solve different problems. One is a real, private tree the browser draws. The other is a description a library like React compares to update the page. Here is how they differ, and how they work together.

Picture a house being renovated. The architect sketches the new layout on paper, lays it over the old sketch, and tells the builder to change only the walls that moved. Meanwhile, one room in the house has its own door and its own wallpaper, and the crew painting the hallway never touches it. The sketch and the room both belong to the house, but one is a drawing and the other is a real room.
The virtual DOM is the sketch. Shadow DOM is the room.
Shadow DOM is a browser feature that gives one element a private tree of real DOM nodes, so the page's CSS rules and queries can't reach inside. The virtual DOM is a pattern some libraries, like React and Vue, use to update the page: they keep a description of the page in JavaScript, compare it with the last one, and change only what differs. Shadow DOM keeps things apart. The virtual DOM works out what to change. They solve different problems, and one page can use both.
What is shadow DOM?
The DOM is the tree of objects the browser builds from your HTML, as the DOM tree lesson shows. Shadow DOM lets you attach a second, private tree to one element:
<h3>Our story</h3>
<div></div>
<script>
const card = document.querySelector("div");
const shadow = card.attachShadow({ mode: "open" });
shadow.innerHTML = "<h3>Green tea</h3>";
console.log(document.querySelectorAll("h3").length); // 1
</script>
The page shows both headings. But the page's query finds only one, because document.querySelectorAll() stops at the edge of the shadow tree. The page's CSS rules stop there too, and the component's own rules don't leak out.
A few names come with it. The element that holds the private tree is the shadow host. The top of that tree is the shadow root, and its edge is the shadow boundary.
Shadow DOM is real. Its nodes are drawn on the page, screen readers read them, and clicks inside reach the page. Browsers even build some of their own elements this way: a <video> hides its play button in a shadow root of the browser's own. What is shadow DOM? builds one step by step, and what encapsulation means lists exactly what crosses the boundary and what doesn't.
What is the virtual DOM?
The virtual DOM isn't a browser feature, and no standard defines it. It's a pattern. Vue's docs describe it as keeping a "virtual" representation of the UI in memory and syncing it with the real DOM, and they credit React with the idea.
Each piece of that representation is a plain JavaScript object that describes an element:
const vnode = {
type: "h3",
props: { class: "name" },
children: ["Green tea"],
};
Nothing here is on the page. It's only data. When your data changes, the library describes the whole screen again, compares the new description with the old one, and changes only the real DOM nodes that differ. That comparison is often called diffing.
React's own docs don't use the term. In React, components return elements, small objects that describe what should be on the screen. React compares them with the last render, then commits only the changes to the DOM. How React works follows one click through those steps, and reconciliation shows how React matches old and new. To write the comparison yourself, try virtual DOM diffing.
How are they different?
| Shadow DOM | Virtual DOM | |
|---|---|---|
| What it is | A real DOM tree attached to one element | Plain JavaScript objects that describe the page |
| Who provides it | The browser, defined in the DOM Standard | A library, like React or Vue |
| The problem it solves | Keeping a component's markup and styles apart from the page | Working out which DOM changes to make when data changes |
| Does the browser draw it? | Yes | No, the library changes the real DOM to match it |
| How you make one | attachShadow(), or a shadow root written in HTML |
You write components, and the library builds it on each render |
| Does it scope CSS? | Yes | No |

Is shadow DOM faster than the virtual DOM?
They don't compete on speed, because they don't do the same job.
A virtual DOM doesn't make the DOM itself faster. It lets you describe the whole screen each time and still touch only the nodes that changed. Vue's docs say its main benefit is letting you build the UI in a declarative way while the library handles the DOM changes.
Shadow DOM isn't a speed feature either. It can cut the browser's style work on a big page, because a shadow root's rules only need checking against the elements inside it. But every shadow root holds its own copy of its styles, unless they all share one constructable stylesheet. When to skip shadow DOM covers that trade, and the advice is the same either way: choose shadow DOM for scoping, and measure your own page.
How do web components update without a virtual DOM?
A web component written by hand changes the real DOM directly. The simplest way, setting innerHTML again on every change, rebuilds every node, so text the user typed and focus are lost. The better way keeps a reference to each part that changes and writes only the new values.
Libraries can do that bookkeeping without a virtual DOM. Lit parses each template once, remembers where the changing values go, and on each update compares every value with the last one it wrote. A changed value goes straight into the same text node. Ways to render a component compares all three approaches and shows Lit's version.
Can you use both together?
Yes, and it's common.
React can render a web component like any other tag. React keeps track of the <tea-card> element it rendered and the props it set. What the component draws inside its own shadow root is up to the component's class.
It works the other way round too. Vue can turn one of its components into a custom element with defineCustomElement(). That element mounts a Vue component inside its own shadow root, so Vue's renderer runs inside the private tree.
What about light DOM?
Light DOM is the third name you'll see. It's an element's ordinary children, the HTML written between its tags, in the page's own tree. When an element has a shadow root, the browser draws the shadow tree instead of those children, unless the shadow tree has a <slot> to show them. Light DOM, shadow DOM, and the flat tree explains how the browser combines them.
Common mix-ups
- "Shadow DOM is a kind of virtual DOM." No. A shadow tree is made of real nodes that the browser draws. A virtual DOM is data a library keeps in JavaScript.
- "The browser has a virtual DOM." No. It's a pattern inside libraries, and each one builds its own.
- "The virtual DOM keeps styles apart." No. In React, CSS is global unless you scope it, for example with CSS Modules.
- "Shadow DOM is a security boundary." No. Any script on the page can reach an open shadow root through
element.shadowRoot.
Where to go next
The web components tutorial teaches shadow DOM from the ground up, starting with what web components are. For the other side, the React course shows how rendering and commits work, from your first component to the internals.


