Sub topic 2
How Emulated encapsulation scopes your CSS
This chapter assumes you can write CSS selectors, know how specificity works, and can build a standalone Angular component with a @Component decorator and an inline template. Here you will learn how Angular keeps a component's styles from leaking, and what that costs you.
The problem encapsulation solves
CSS rules are global in the browser. A rule like .label { color: red; } applies to every element with that class, wherever it appears. In a large app, two components that both use .label would collide.
Angular's default mode, ViewEncapsulation.Emulated, fixes this without using the browser's native Shadow DOM. Angular rewrites your selectors at build time and adds matching attributes to the template's elements at render time.
What Angular does to your CSS and your template
Take the badge component below.
import { Component } from '@angular/core';
@Component({
selector: 'app-badge',
template: `<span class="label"><ng-content /></span>`,
styles: `
:host {
display: inline-block;
}
.label {
padding: 2px 8px;
border-radius: 4px;
background: #e0e7ff;
}
`,
})
export class BadgeComponent {}Angular gives each component a unique identifier. It then makes two changes.
- It adds attributes to the elements in the component's own template. The host element gets an attribute such as
_nghost-abc-c1. Elements inside the template get_ngcontent-abc-c1. - It rewrites each selector in the component's styles to require that attribute.
The rendered DOM and the processed CSS look roughly like this. The exact attribute names vary between builds and versions, so never depend on them.
<app-badge _nghost-abc-c1>
<span class="label" _ngcontent-abc-c1>New</span>
</app-badge>[_nghost-abc-c1] {
display: inline-block;
}
.label[_ngcontent-abc-c1] {
padding: 2px 8px;
border-radius: 4px;
background: #e0e7ff;
}The .label rule now matches only elements that carry this component's attribute. A .label in another component's template has a different attribute, so the rule does not touch it.
What is and is not scoped
Emulated encapsulation scopes styles in one direction. Your component's styles do not leak out, but outside styles can still get in.
| Direction | Behavior under Emulated |
|---|---|
| Component styles to other components | Blocked. The rewritten selectors need this component's attribute. |
| Global styles to this component | Allowed. A global `.label` rule still matches your `span`. |
| Parent styles to a child component's template | Blocked. The child's elements carry only the child's attribute. |
| Inheritable properties such as `color` and `font-family` | Pass down through the DOM as usual. |
The last row catches many people. Encapsulation changes which selectors match. It does not stop CSS inheritance, so a color set on a parent still flows into the child unless the child sets its own.
Common errors
- Styling a child's internals from the parent. A parent rule such as
app-badge .label { color: red; }has no effect. The.labelelement carries the child's attribute, and the parent's rewritten selector requires the parent's attribute. Style the child through its own styles, through CSS custom properties, or through global styles. - Styling projected content. Content passed through
<ng-content>belongs to the component that wrote it in its template, not to the component that hosts the slot. Style it in the author's component. - Expecting full isolation. A global stylesheet can still restyle your elements. Only
ViewEncapsulation.ShadowDomblocks that, and a later chapter covers it. - Relying on attribute names. Selectors such as
[_ngcontent-abc-c1]in tests or scripts will break when the build changes.
Check your understanding
Exercise
A parent component's stylesheet contains app-badge .label { background: red; }. The badge's template contains <span class="label">. Under the default encapsulation, does the rule apply? Why or why not?
Show answer
No. Emulated encapsulation rewrites the parent's selector so that .label must carry the parent's generated attribute. The span inside the badge carries the badge's attribute instead, so the selector never matches. To change the color, use the badge's own styles, a CSS custom property the badge reads, or a global style.
Exercise
A global stylesheet sets p { margin: 0; }. A component with default encapsulation renders a <p>. Does the global rule apply to it, and does the component's own p { margin: 8px; } win?
Show answer
Yes, the global rule can apply, because Emulated encapsulation does not block global styles. The component's rule also applies, and it wins because its rewritten selector is p[_ngcontent-abc-c1], which has higher specificity than p.