Sub topic 9
ViewEncapsulation.None: styles that leak on purpose
This chapter assumes you can write a standalone Angular component with the @Component decorator, you know CSS selectors and specificity, and you have read the earlier chapters on Emulated encapsulation and :host. Here you will learn what ViewEncapsulation.None does, when it is justified, how it fails, and which alternatives are usually safer.
What None does
By default, Angular uses Emulated encapsulation. It adds generated attributes such as _ngcontent-abc-c1 to the component's elements and rewrites each selector in the component's styles to require those attributes. The result is that a rule written for one component cannot match elements in another.
ViewEncapsulation.None turns this off. Angular adds no attributes to the template's elements and does not rewrite the selectors. It copies your CSS into the document <head> exactly as you wrote it. The rules then behave like any global stylesheet and can match any element on the page, inside or outside the component.
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-article',
template: `
<article class="prose">
<h2>Title</h2>
<p>Body text.</p>
</article>
`,
styles: `
.prose p {
line-height: 1.7;
max-width: 65ch;
}
`,
encapsulation: ViewEncapsulation.None,
})
export class ArticleComponent {}The .prose p rule now applies to every <p> inside any element with the class prose, wherever it appears in the application.
When the styles are applied
Angular adds a component's styles to the document when the first instance of the component is created. With Emulated and ShadowDom encapsulation, Angular can remove the styles again when the last instance is destroyed. With None, the styles stay in the document once added and remain active until the page is closed. This has two consequences:
- A rule can affect other parts of the page long after the component has left the screen.
- The rules do not exist until the component has rendered at least once. A page that never creates the component never receives its styles.
The second point matters when you rely on a component to supply styles for other components. If that component is lazy-loaded, the styles will not be there until the route is visited. That makes the behavior depend on navigation order, and it is hard to debug.
How leaking affects the cascade
A None component joins the normal CSS cascade as a global stylesheet. Two things decide which rule wins: specificity and source order.
Emulated rules gain extra specificity, because the attribute selector counts as an attribute selector in the cascade. A plain class selector in a None component has lower specificity than the same rule in an Emulated component. Consider these two components:
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-leaky',
template: `<p class="note">Leaky</p>`,
styles: `.note { color: crimson; }`,
encapsulation: ViewEncapsulation.None,
})
export class LeakyComponent {}
@Component({
selector: 'app-scoped',
template: `<p class="note">Scoped</p>`,
styles: `.note { color: navy; }`,
})
export class ScopedComponent {}When both components exist on the page, .note in ScopedComponent is rewritten to .note[_ngcontent-xyz-c2], which is more specific than the leaky .note. The scoped paragraph stays navy. The leaky rule, however, still matches any .note element in the application that lacks a more specific rule. A third component with <p class="note"> and no styles of its own would turn crimson. The reverse never happens. Emulated rules cannot reach into the None component.
The order in which styles are added also decides ties between two None components. The component created later adds its styles later, and for equal specificity the later rule wins. That order depends on which component renders first, so the result can change when you reorder routes or change lazy loading.
Legitimate uses
ViewEncapsulation.None is a good fit in a small number of situations.
- A component that renders HTML you do not control. If a component inserts markup with
[innerHTML](for example rendered Markdown or a CMS body), Angular does not add the generated attributes to those elements. Emulated rules therefore cannot match them. Setting None lets you style the generated tags such ash2,aandcode. - A component that creates elements by hand. Content added with
Renderer2,document.createElement, or a third-party library is also missing the generated attributes. A None component can style it. - A styling layer you want to be global but keep next to a component. A layout shell or theme component can hold the rules for the whole application. Keep this to one clearly named component, and remember that a global stylesheet (covered in the global styles chapter) usually does the same job with less surprise.
- Styles that must reach a third-party widget's internal elements. If the widget's DOM is built outside Angular's templates, a None component can reach those elements without
::ng-deep.
Here is the first case in code. The sanitized HTML comes from a signal, so the component re-renders when the content changes.
import { Component, ViewEncapsulation, input } from '@angular/core';
@Component({
selector: 'app-rich-text',
template: `<div class="rich-text" [innerHTML]="html()"></div>`,
styles: `
app-rich-text .rich-text h2 {
margin-block: 1.5rem 0.5rem;
}
app-rich-text .rich-text a {
text-decoration: underline;
}
app-rich-text .rich-text code {
background: #f1f5f9;
padding: 0 4px;
}
`,
encapsulation: ViewEncapsulation.None,
})
export class RichTextComponent {
html = input.required<string>();
}Notice that every selector starts with app-rich-text. This prefix is the key technique for using None safely, and the next section explains why.
Containing the leak by hand
With None, Angular no longer scopes your CSS, so you have to do it yourself. These habits reduce the risk:
- Prefix every selector with the host element's tag name.
app-rich-text .rich-text h2can only match inside<app-rich-text>. This reproduces most of what Emulated does, but it applies to every element in the subtree, including those created by hand or byinnerHTML. - Use specific class names. A name such as
.rich-textcollides less often than.titleor.button. A naming convention such as BEM (block__element--modifier) helps. - Do not style bare element selectors without a prefix. A rule like
h2 { color: red; }in a None component changes everyh2in the application. - Put theme values in custom properties. A None component that sets variables on
:rootor on its own host lets other code adjust values without new selectors. The theming chapter covers this approach in detail.
Compare an unsafe rule with a contained one:
/* Unsafe: applies to every table in the application */
table {
border-collapse: collapse;
}
/* Contained: applies only inside the host element */
app-data-grid table {
border-collapse: collapse;
}Failure modes
The following problems appear most often in applications that use None.
| Problem | Cause | Fix |
|---|---|---|
| A button elsewhere changes color | An unprefixed `.btn` rule in a None component | Prefix with the host tag name or rename the class |
| Styles appear only after visiting a route | The component that supplies them is lazy-loaded | Move shared rules to a global stylesheet |
| A rule works in one build and fails in another | Two None components with equal specificity depend on creation order | Raise specificity deliberately or remove the overlap |
| Styles remain after the component is gone | None styles are not removed when the component is destroyed | Treat them as global and design for that |
| Tests pass but production looks different | Unit tests render one component at a time, so no other rule competes | Add an integration or visual test |
| A `:host` rule has no effect | The host has no generated attribute under None | Use the tag name or a class selector |
Server-side rendering and hydration
With server-side rendering, Angular writes the <style> elements into the HTML it sends. On the client, hydration reuses these elements rather than adding duplicates. Because None styles are global, they are always present once rendered. This is simple, but it also means you cannot rely on the server output to scope them. If you test a page with SSR enabled, check that the same rules apply before and after hydration.
Safer alternatives
Before choosing None, check whether one of these meets the need:
- CSS custom properties. If the goal is to let a parent change colors or spacing, expose variables with fallbacks (
var(--card-padding, 16px)). They cross encapsulation boundaries without any leaking selectors. - Global styles. If a rule really should apply everywhere, put it in
styles.csswhere the team expects to find it. Hiding a global rule inside a component makes it hard to locate. :host ::ng-deep. For styling a child component's internals, this keeps the rule limited to descendants of one component. It is deprecated, so treat it as a temporary measure.- Class and style bindings on elements you own. If you can add a class to the element, Emulated encapsulation works unchanged and no leaking is needed.
ViewEncapsulation.ShadowDom. If you need the opposite, strict isolation, choose the native shadow root instead.
A rough decision order is: first custom properties, then global styles, then Emulated with bindings, and only then None for content that Angular does not own.
Setting the default for a whole application
You can also change the default mode for every component without a per-component encapsulation property. The @Component decorator is not the only place. Providing a different default in the application configuration affects all components that do not set their own value. Doing this for a whole app removes isolation everywhere, so it is rarely advisable. A single, well-named component with None is easier to audit than an application where every stylesheet is global.
Exercise
A component uses ViewEncapsulation.None and has the rule p { margin: 0; }. Name two effects this has on the rest of the application, and rewrite the rule so that it applies only inside the component, whose selector is app-note.
Show answer
First, every <p> in the application, in any component, loses its margin, because the selector is copied unchanged into the document head. Second, the rule stays in the document after the component is destroyed, and it exists only after the component has rendered at least once, so the visual result can depend on navigation order. Rewrite it as app-note p { margin: 0; }, which matches only paragraphs inside the <app-note> host element.
Exercise
A component renders Markdown with [innerHTML]. Its Emulated style h2 { color: teal; } has no effect on the generated headings. Why, and what is the least leaky change that fixes it?
Show answer
Angular adds its generated attribute only to elements created from the component's template. Elements inserted through [innerHTML] do not get that attribute, so the rewritten selector h2[_ngcontent-...] never matches them. The least leaky fix is to set encapsulation: ViewEncapsulation.None on that component and prefix the selector with the host tag, for example app-markdown h2 { color: teal; }. Using :host ::ng-deep h2 would also work, but it relies on a deprecated feature.