Sub topic 10
ViewEncapsulation.ShadowDom: native style isolation
This chapter assumes you know the default Emulated encapsulation, basic CSS selectors and specificity, and how to write a standalone Angular component. You should also know that :host targets the element a component is attached to.
What ShadowDom mode does
Emulated encapsulation fakes isolation. Angular adds generated attributes to elements and rewrites your selectors to match them. ViewEncapsulation.ShadowDom uses the browser's own mechanism instead. Angular calls attachShadow() on the component's host element and renders the template inside the resulting shadow root. The browser then enforces the boundary.
Isolation works in both directions:
- Global styles and styles from parent components do not reach elements inside the shadow root.
- The component's own styles do not leak out to the rest of the page.
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-widget',
template: `<p class="note">Isolated by a shadow root</p>`,
styles: `.note { color: darkgreen; }`,
encapsulation: ViewEncapsulation.ShadowDom,
})
export class WidgetComponent {}If a global stylesheet contains p { color: red; }, the paragraph above stays dark green. It would not stay green under Emulated mode, where a global rule can still apply to elements inside the component.
How it differs from Emulated
In the DOM, an Emulated component's elements carry attributes such as _ngcontent-abc-c12. A ShadowDom component's elements carry no such attributes. Its content sits under a #shadow-root node in the browser's developer tools, and the component's styles are placed inside that root.
| Aspect | Emulated | ShadowDom |
|---|---|---|
| Mechanism | Generated attributes and rewritten selectors | Native shadow root |
| Global styles reach inside | Yes | No |
| Component styles leak out | No | No |
| `:host` | Rewritten by Angular | Native CSS |
| Inherited properties and custom properties cross the boundary | Yes | Yes |
What still crosses the boundary
Isolation applies to selector matching. It does not stop inheritance. Properties such as color, font-family and line-height are inherited by default, so a shadow tree picks them up from its host's ancestors unless the component sets them itself. CSS custom properties are inherited too, which is why they are the standard way to theme a shadow-isolated component.
/* styles.css (global) */
app-widget {
--widget-accent: rebeccapurple;
}Inside the component, read the value with a fallback: color: var(--widget-accent, darkgreen);. The parent decides the value, and the component decides where to use it.
Gotchas
- Global CSS libraries stop working inside the component. Utility classes and framework stylesheets do not apply to the shadow tree. You must import the CSS the component needs into the component's own styles, which duplicates it for every ShadowDom component that does so.
::ng-deephas nothing to pierce. A shadow root blocks selectors from outside, so the technique does not help. Use custom properties, or the CSS::part()selector if you expose parts with thepartattribute.- Browser behavior governs the result. Styling form controls, focus handling and the way events are retargeted at the boundary follow the native shadow DOM rules, not Angular's.
- Debugging is harder. Your own
querySelectorcalls ondocumentwill not find elements inside the shadow root.
When to choose it
ShadowDom fits components that must look the same wherever they are embedded, such as widgets shipped to pages you do not control, or components whose own styles must never be affected by the host page. For ordinary application components, Emulated is simpler and works with your global stylesheet, so keep it as the default.
Exercise
A component uses ViewEncapsulation.ShadowDom. A global stylesheet contains p { color: red; } app-widget { --accent: teal; }. The component template has a <p> styled with color: var(--accent, black);. What color is the paragraph, and why?
Show answer
Teal. The global p rule cannot match inside the shadow root, so it is ignored. The custom property --accent is inherited across the boundary from the app-widget host, so var(--accent, black) resolves to teal. The black fallback applies only if no parent sets the property.
Exercise
Name two things that still cross a shadow root boundary, and one thing that does not.
Show answer
Inherited properties such as font-family and CSS custom properties cross it. Selector-based rules from global or parent stylesheets, such as p { color: red; }, do not.