Sub topic 9
ViewEncapsulation.None: styles that leak on purpose
This chapter assumes you know the default Emulated encapsulation, can write a standalone component with a styles array or string, and understand CSS specificity. Here you will see what ViewEncapsulation.None does, when it is justified, and how its leaks cause bugs.
What None does
With Emulated encapsulation, Angular adds generated attributes such as _ngcontent-abc-c1 to your elements and rewrites each selector to match them. With ViewEncapsulation.None, Angular skips both steps. It adds no attributes and does not rewrite selectors. The component's CSS goes into the page as ordinary global CSS.
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-article',
template: `
<article class="article">
<h2>Release notes</h2>
<p>Styles from this component apply everywhere.</p>
</article>
`,
styles: `
.article h2 {
color: darkslateblue;
}
.article p {
line-height: 1.6;
}
`,
encapsulation: ViewEncapsulation.None,
})
export class ArticleComponent {}The .article h2 rule now matches any .article h2 in the whole document, including elements in other components. The reverse also holds. Global styles and rules from other None components can change this template.
When the leak is useful
Three cases justify None.
- Content you do not template. HTML from
innerHTMLor a Markdown renderer does not receive Emulated attributes, so scoped rules never match it. WithNone, the rules match. - Third-party markup. A library may create DOM that Angular does not compile, such as a chart or an editor. Its elements also lack the Emulated attributes.
- Shared base styles. A single top-level component can ship a small set of styles for the whole app, like a reset or typography, without a separate stylesheet entry.
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-markdown-view',
template: `<div class="md" [innerHTML]="html"></div>`,
styles: `
.md h1 { font-size: 1.75rem; }
.md code { background: #f1f5f9; padding: 0 4px; }
`,
encapsulation: ViewEncapsulation.None,
})
export class MarkdownViewComponent {
html = '<h1>Title</h1><p>Use <code>None</code> carefully.</p>';
}Angular sanitizes innerHTML values. Keep that in mind when the content comes from users.
How the leak behaves
Three details matter in practice.
- Lifetime. Angular adds the styles when the first instance of the component is created. It does not remove them when the component is destroyed. The rules stay in the page for the rest of the session, so a lazy-loaded
Nonecomponent can restyle the whole app after the user visits its route. - Order. Styles are added in creation order. For equal specificity, the later rule wins, so the result depends on which component rendered first.
- Selectors.
:hosthas nothing to match when there is no encapsulation. Angular still processes it, but you should select the element by its tag name instead, such asapp-article { display: block; }.
Common errors
- Bare element selectors.
h2 { color: red; }in aNonecomponent changes everyh2in the app. Prefix every rule with a class that belongs to the component. - Generic class names.
.titleor.buttonwill collide with other components. Use a distinct prefix, for example.article-title. - A bug that appears only after navigation. If a page looks different depending on which route you visited first, look for a
Nonecomponent that has loaded. - Using
Noneto fix a one-off override. To change a child component's look, prefer a CSS custom property (covered in an earlier chapter). Reach forNoneonly when custom properties cannot do the job.
Quick comparison
| Mode | Attributes added | Rules reach other components | Global styles reach this template |
|---|---|---|---|
| Emulated | Yes | No | Yes |
| None | No | Yes | Yes |
| ShadowDom | No (uses a shadow root) | No | No |
Exercise
A lazy-loaded route component uses ViewEncapsulation.None and has the rule p { margin: 0; }. After a user visits that route and goes back to the home page, paragraphs on the home page have lost their margins. Why does this happen, and how do you fix it?
Show answer
With None, the rule is added to the document as a global style when the component is first created, and Angular does not remove it when the component is destroyed. The bare p selector then matches every paragraph in the app. Fix it by scoping the rule to a class owned by the component, such as .report p { margin: 0; } with class="report" on the component's root element. Alternatively, return to the default Emulated encapsulation if the component does not render unmanaged HTML.