Beginner 15 min read

Web Standards Behind LWC: Web Components and Shadow DOM

By the end of this lesson, you'll be able to:

  • Name the four core Web Components browser APIs LWC is built on
  • Explain what the Shadow DOM is and why LWC uses it
  • Predict how Shadow DOM affects CSS and DOM queries across components

Prerequisites: "LWC vs. Aura Components"

The Four Pillars of Web Components

  • Custom Elements — lets you define new HTML tags, like <c-my-component>.
  • Shadow DOM — style and DOM encapsulation, covered in depth below.
  • HTML Templates — the <template> tag: inert, reusable markup that's only parsed when explicitly used.
  • ES Modules — native JavaScript's own import/export system, no bundler-specific magic required.

LWC layers Salesforce-specific tooling — decorators, the wire service, base components — on top of these four standards. It doesn't replace them; it builds on them.

What Is the Shadow DOM, and Why Does It Matter?

On a normal webpage, CSS and DOM queries are global — a style rule written anywhere can affect any element anywhere on the page. Shadow DOM creates an encapsulated boundary around a component: its internal markup and styles are isolated from the rest of the page, and vice versa. Your component's CSS can't accidentally leak out and break someone else's component, and outside CSS can't accidentally reach in and override yours.

This matters enormously in Salesforce specifically, where many components from many different sources — Salesforce's own base components, your custom LWCs, possibly AppExchange package components — all render on the very same page at once.

How This Plays Out in Practice

// From OUTSIDE a component (e.g. the browser console, or a parent
// component's own code), this will NOT find elements rendered
// inside another component's shadow boundary:
document.querySelector('.some-class-inside-a-component');
// returns null — the shadow boundary blocks it.

To interact with a component from outside, you go through its deliberately-exposed public API (its @api properties and methods) instead of reaching into its internals — full detail in Module 5.

Why This Matters in Real Projects

Shadow DOM prevents an entire category of bug: "why did installing this AppExchange package break the styling on my custom page?" It makes components genuinely reusable and composable without requiring careful naming coordination between every team that ever builds a component for the org. It's also part of why LWC performs well — browsers optimize natively for this pattern, rather than Salesforce having to reimplement isolation in JavaScript on top.

Exercise

Explain, as a comment, why two completely different LWC components can both safely use a CSS class named .title without any conflict.

Show hint

Think about what the Shadow DOM boundary actually isolates.

JAVASCRIPT

Exercise

Challenge: a teammate asks you to write CSS in ComponentA that changes the background color of a <div> living inside a child ComponentB. Explain why a plain CSS selector won't work, without needing to write the full solution.

Show hint

Recall what Shadow DOM blocks from outside a component.

JAVASCRIPT

Web Standards Behind LWC: Web Components and Shadow DOM Quiz

1. Which of these is NOT one of the four Web Components browser standards LWC is built on?

2. What does Shadow DOM primarily provide?

3. A document.querySelector() call from outside a component can freely reach elements deep inside that component's shadow tree, just like on a normal page.

4. Two independently-built components both define a CSS class called .container for layout. What happens without Shadow DOM encapsulation?

5. Why does Shadow DOM matter more on the Salesforce platform specifically than on many single-team websites?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

LWC layers Salesforce-specific tooling on top of four native browser standards — and Shadow DOM, the encapsulation piece, is the reason many components from different sources can safely coexist on the same Salesforce page.