Introduction to the Shadow DOM

The shadow DOM is a standard Web API, allowing that some parts of the DOM that is generated when rendering a Web page are isolated from the other parts of the page.

This specification is part of the Web Components framework, and there applied to LWC.

When using LWC, every component is encapsulated using a shadow DOM by default, because the class LightningElementfrom where most of the LWCs created are inherited already implements the Shadow DOM concept.

export default class YourLwcComponent extends LightningElement {
...
}

The most important benefits of using LWC and the Shadow DOM are:

As you can see those benefits can be summarized in 2: Security and Encapsulation.

Vocabulary used

<aside> ☝ You may hear in old blog/videos posts about the synthetic polyfill, from 2023 this is not relevant anymore (this was used by Salesforce to apply the LWC DOM shadowing to old browsers).

</aside>

It might be useful to activate this setting in the Developer Tools if you use LWC OSS on your browser of choice, but not in LWC custom development:

Untitled

Querying the DOM