--- url: "https://web.dev/articles/shadowdom" titulek: "Shadow DOM 101 | Articles | web.dev" h1: "Shadow DOM 101 Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/shadowdom" drobecky: ["Articles", "Shadow DOM 101"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:03:11" znaku: 14228 slov: 2162 kodovani: "iso-8859-2" sha256: "b9d05ab7f297bc99053c155ae7ef2e8d0c07e32b543c3839f04b6f9b62a85b4e" --- Shadow DOM 101 Stay organized with collections Save and categorize content based on your preferences. Dominic Cooney Introduction Web Components is a set of cutting edge standards that: Make it possible to build widgets …which can be reused reliably …and which won’t break pages if the next version of the component changes internal implementation details. Does this mean you have to decide when to use HTML/JavaScript, and when to use Web Components? No! HTML and JavaScript can make interactive visual stuff. Widgets are interactive visual stuff. It makes sense to leverage your HTML and JavaScript skills when developing a widget. The Web Components standards are designed to help you do that. But there is a fundamental problem that makes widgets built out of HTML and JavaScript hard to use: The DOM tree inside a widget isn’t encapsulated from the rest of the page. This lack of encapsulation means your document stylesheet might accidentally apply to parts inside the widget; your JavaScript might accidentally modify parts inside the widget; your IDs might overlap with IDs inside the widget; and so on. Web Components is comprised of three parts: [Templates](https://html.spec.whatwg.org/multipage/scripting.html#the-template-element) [Shadow DOM](https://www.w3.org/TR/shadow-dom/) [Custom Elements](https://html.spec.whatwg.org/multipage/custom-elements.html) **Shadow DOM** addresses the DOM tree encapsulation problem. The four parts of Web Components are designed to work together, but you can also pick and choose which parts of Web Components to use. This tutorial shows you how to use Shadow DOM. Hello, Shadow World With Shadow DOM, elements can get a new kind of node associated with them. This new kind of node is called a **shadow root**. An element that has a shadow root associated with it is called a **shadow host.** The content of a shadow host isn’t rendered; the content of the shadow root is rendered instead. For example, if you had markup like this: ` ` then instead of ` ` your page looks like ` ` Not only that, if JavaScript on the page asks what the button’s `textContent` is, it isn’t going to get “こんにちは、影の世界!”, but “Hello, world!” because the DOM subtree under the shadow root is encapsulated. Separating Content from Presentation Now we'll look at using Shadow DOM to separate content from presentation. Let's say we have this name tag: `
Hi! My name is
Bob
` Here is the markup. This is what you’d write today. It doesn’t use Shadow DOM: `
Hi! My name is
Bob
` Because the DOM tree lacks encapsulation, the entire structure of the name tag is exposed to the document. If other elements on the page accidentally use the same class names for styling or scripting, we're gonna have a bad time. We can avoid having a bad time. Step 1: Hide Presentation Details Semantically we probably only care that: It is a name tag. The name is “Bob”. First, we write markup that is closer to the true semantics we want: `
Bob
` Then we put all of the styles and divs used for presentation into a `