Sub topic 2
How Emulated encapsulation scopes your CSS
This chapter assumes you can write CSS selectors and reason about specificity, and that you can build a standalone Angular component with a @Component decorator and a template. It explains what Angular actually does to your CSS under the default ViewEncapsulation.Emulated mode, so you can predict which styles apply and debug the ones that don't.
The problem Emulated mode solves
CSS rules are global by default. If one component defines .label { color: red; }, every element with the class label on the page turns red. In a large app with many teams and third-party libraries, this causes collisions.
Browsers have a native solution, Shadow DOM, but Angular does not use it by default. Emulated mode gives you similar scoping with plain CSS, so it works in every browser and still lets global styles reach into components. It is the default, so you get it unless you set encapsulation yourself.
What Angular does at build time
Emulated mode is two source transformations, one on the template and one on the CSS.
- Angular generates a unique identifier for each component type.
- Every element in that component's template gets an attribute containing the identifier, such as
_ngcontent-ng-c2891042. - The component's host element (the element matched by its selector) gets a different attribute, such as
_nghost-ng-c2891042. - Every selector in the component's styles is rewritten to also require the content attribute.
Take the badge component:
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 {}At runtime the browser receives roughly this DOM and CSS. The identifier number differs per component and per build.
<app-badge _nghost-ng-c2891042>
<span class="label" _ngcontent-ng-c2891042>New</span>
</app-badge>[_nghost-ng-c2891042] {
display: inline-block;
}
.label[_ngcontent-ng-c2891042] {
padding: 2px 8px;
border-radius: 4px;
background: #e0e7ff;
}The rule .label[_ngcontent-ng-c2891042] matches only elements that have both the class and the attribute. A .label element in any other component's template lacks that attribute, so the rule ignores it.
Where the styles end up
Angular does not copy the CSS for every component instance. It adds one <style> element to the document <head> the first time the component type is rendered. All instances of that component share it. When the last instance is destroyed, Angular may remove the style element.
The practical consequence is that the CSS is still global in the browser's eyes. Only the attribute requirement makes it behave as if it were local. This explains several behaviors in the next sections.
Specificity increases slightly
An attribute selector counts the same as a class selector in specificity. Rewriting .label to .label[_ngcontent-ng-c2891042] raises its specificity from (0,1,0) to (0,2,0). A bare element rule such as p { color: blue; } becomes p[_ngcontent-ng-c2891042], which is (0,1,1) instead of (0,0,1).
This is why a component rule often beats a global rule that looks equally specific:
/* styles.css (global) */
.label {
background: pink;
}If the badge's own .label rule sets background: #e0e7ff, the component rule wins, because .label[_ngcontent-...] outranks the plain .label. You don't need !important for a component to override a global default. It also means a global stylesheet cannot easily override a component's own rules without raising its specificity.
Parent styles do not reach into child templates
Consider a parent that uses the badge:
import { Component } from '@angular/core';
import { BadgeComponent } from './badge.component';
@Component({
selector: 'app-toolbar',
imports: [BadgeComponent],
template: `
<h2 class="title">Inbox</h2>
<app-badge>3</app-badge>
`,
styles: `
.title { margin: 0; }
.label { color: red; }
app-badge { margin-left: 8px; }
`,
})
export class ToolbarComponent {}.titleapplies, because the<h2>is in the toolbar's template and has the toolbar's_ngcontentattribute..labeldoes nothing. The<span class="label">lives in the badge's template and carries the badge's attribute, not the toolbar's. The rewritten selector.label[_ngcontent-toolbar]cannot match it.app-badgeworks. The<app-badge>element is written in the toolbar's template, so it receives the toolbar's_ngcontentattribute. It also carries the badge's_nghostattribute. The parent can therefore style the child's host element, though not its internals.
The rule of thumb is that a component's styles apply to elements declared in that component's own template. This includes the host elements of children, but not the children's contents.
Projected content belongs to the declaring template
Content projection with <ng-content> follows the same rule. Nodes you place between a component's tags are declared in the parent's template. They keep the parent's attribute even though they render inside the child's DOM.
@Component({
selector: 'app-page',
imports: [BadgeComponent],
template: `<app-badge><strong>Sale</strong></app-badge>`,
styles: `strong { text-transform: uppercase; }`,
})
export class PageComponent {}The <strong> element is styled by PageComponent, because it is declared there. If BadgeComponent defined strong { ... } in its own styles, that rule would not apply to it, even though the element appears inside the badge's <span>. To style projected content from the child, you need a different mechanism, such as ::slotted under ShadowDom or a CSS custom property that the child reads.
Content that Angular did not create
The attribute is added when Angular's renderer creates the element. Markup that arrives some other way has no attribute, so scoped rules never match it. Typical cases are:
- HTML bound with
[innerHTML]. - Elements created by a third-party library, such as a chart or rich-text editor, that appends nodes to the DOM directly.
- Nodes you add yourself with
document.createElementorelement.innerHTML = ....
import { Component } from '@angular/core';
@Component({
selector: 'app-article',
template: `<div [innerHTML]="body"></div>`,
styles: `
/* Never matches: injected <p> elements have no _ngcontent attribute. */
p { line-height: 1.6; }
`,
})
export class ArticleComponent {
body = '<p>Injected paragraph</p>';
}The <div> gets the attribute because it is in the template. The <p> inside it does not. Angular's sanitizer will also strip some attributes and tags from innerHTML content, which is a separate concern. For these cases you typically use a global style, :host ::ng-deep (covered in a later chapter), or the library's own theming options.
Global styles still apply
Emulated mode restricts component styles from leaking out. It does not stop outside styles from leaking in. A rule in styles.css such as p { margin: 0; } has no attribute requirement, so it matches every <p> in every component. This is deliberate and is what makes reset stylesheets and design-system base styles work. It also means encapsulation in Emulated mode is one-directional. The global styles chapter returns to this.
How Emulated compares with the other modes
| Behavior | Emulated | ShadowDom | None |
|---|---|---|---|
| Component styles leak out | No | No | Yes |
| Global styles reach in | Yes | No | Yes |
| Mechanism | Generated attributes and rewritten selectors | Native shadow root | None, styles are global |
| Works without browser Shadow DOM support | Yes | No | Yes |
Debugging checklist
When a component style does not behave as expected, work through these questions in order.
- Open DevTools and confirm the target element has the
_ngcontent-...attribute that matches your component's rewritten rule in the<head>. - If the element belongs to a child component, the rule cannot reach it. Style the child from inside, or pass values down with CSS custom properties.
- If the element is projected content, check which template declares it. That template's styles apply.
- If the element came from
innerHTMLor a library, it has no attribute. Use a global style or a deliberate piercing selector. - If a global rule is winning or losing unexpectedly, compare specificity. Remember the extra attribute adds (0,1,0) to component rules.
Exercise
A parent component's stylesheet contains .label { color: red; }. Its template includes <app-badge>, and the badge's own template contains <span class="label">. Why does the text stay unstyled, and which element in the parent's template could this parent style directly?
Show answer
Under Emulated encapsulation, Angular rewrites the parent's rule to .label[_ngcontent-parent-id]. The span lives in the badge's template, so it carries the badge's _ngcontent attribute, not the parent's, and the rule cannot match it. The parent can style the <app-badge> host element itself, because that element is declared in the parent's template and receives the parent's attribute.
Exercise
A component sets [innerHTML]="html" on a <div> where html is '<em>hi</em>', and its styles contain em { color: crimson; }. The text does not turn crimson. Explain why, and name one way to style the injected element.
Show answer
The <em> element was inserted through innerHTML, not created by Angular's renderer from the template, so it has no _ngcontent-... attribute. The rewritten selector em[_ngcontent-...] therefore never matches it. You can style it with a global stylesheet rule, or with :host ::ng-deep em { ... } inside the component, which removes the attribute requirement for descendants of the host.