Sub topic 5
Choosing a ViewEncapsulation mode
This chapter assumes you can write a standalone Angular component with a @Component decorator, and that you know CSS selectors and specificity. You should also know the default Emulated behavior from the previous chapter, where each component's styles are scoped to its own template.
What view encapsulation controls
View encapsulation decides how far a component's CSS reaches and how much outside CSS reaches in. You set it with the encapsulation property of @Component, using the ViewEncapsulation enum. Angular offers three modes.
| Mode | Scoping mechanism | Global styles reach in? | Component styles leak out? |
|---|---|---|---|
| `Emulated` (default) | Angular rewrites selectors with generated attributes | Yes | No |
| `ShadowDom` | Browser-native shadow root | No | No |
| `None` | No scoping at all | Yes | Yes |
Emulated: the default
With Emulated, Angular adds a generated attribute to each element in the template. It rewrites your selectors to match that attribute. A .label rule then matches only elements rendered by that component's template.
The isolation works in one direction. Your component styles stay inside, but global styles and styles from other components can still match your elements by normal CSS rules. For most applications this is the right choice, so change it only when you have a specific reason.
ShadowDom: native isolation
ViewEncapsulation.ShadowDom renders the template inside a browser shadow root. The browser enforces the boundary. Global stylesheets cannot match elements inside, and the component's own styles cannot match anything outside.
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 {}Choose this mode when a component must be protected from the surrounding page. Examples are a widget embedded in pages you don't control, or a component packaged as a custom element. Be aware of two costs. Global styles such as a CSS reset or a shared typography sheet stop applying inside the component. Also, CSS custom properties are the main way to pass styling in, because inherited properties still cross the shadow boundary.
None: styles that leak on purpose
ViewEncapsulation.None turns scoping off. Angular adds the component's styles to the document as ordinary global CSS.
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-article',
template: `<h2>Title</h2><p>Body text</p>`,
styles: `
app-article h2 { margin-bottom: 4px; }
`,
encapsulation: ViewEncapsulation.None,
})
export class ArticleComponent {}Every rule now applies to the whole page. A bare h2 selector here would restyle every h2 in the application, including in unrelated components. If you use None, prefix your selectors with the component's element name or a unique class, as the example does.
The mode is useful when a component renders HTML you don't control, such as content from a Markdown renderer, and you need to style elements that carry no Angular attribute. It is also a way to ship a theme from a component.
How the choice plays out
The diagram shows how a global rule and a component rule interact in each mode.
Decision criteria
- Start with Emulated. It gives scoped styles and lets your global theme and reset apply.
- Use ShadowDom when you need a hard boundary enforced by the browser, and accept that global styles won't apply inside.
- Use None only when scoping gets in the way, and write selectors that are narrow enough that they can't hit unrelated elements.
- Before switching modes to restyle a child, check whether CSS custom properties solve the problem. They work across Emulated and ShadowDom boundaries without weakening isolation.
Common errors
- Switching to
Noneto fix one stubborn child style, then finding that unrelated pages changed. - Expecting a global stylesheet to style elements inside a ShadowDom component.
- Using
Nonewith a bare element selector such asbutton, which rewrites every button in the app.
Exercise
A component uses ViewEncapsulation.ShadowDom, and your global styles.css sets p { color: red; }. Will the <p> inside the component's template turn red? How could you still let a parent control its text color?
Show answer
No. The shadow root blocks global selectors from matching elements inside it. To let a parent control the color, have the component read a CSS custom property, for example color: var(--note-color, darkgreen);, and set --note-color on the component's host element from outside. Custom properties are inherited across the shadow boundary.
Exercise
Why is h2 { margin: 0; } risky in a component that uses ViewEncapsulation.None, and how would you make it safer?
Show answer
With None, the rule is added as global CSS, so it applies to every h2 in the application. Make it safer by scoping the selector, for example app-article h2 { margin: 0; }, or by using a unique class name.