Sub topic 14
Styling Components and View Encapsulation
This chapter assumes you know component anatomy (the @Component decorator, selector, template, and class) and basic CSS selectors. It covers how Angular keeps a component's styles from leaking out, and the mistakes that follow from that.
Where Component Styles Live
A component can declare CSS inline with styles or in a separate file with styleUrl. Both are scoped to that component.
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-badge',
template: `<span class="label"><ng-content /></span>`,
styles: `
:host { display: inline-block; }
:host(.warning) .label { background: orange; }
.label { padding: 0.2rem 0.5rem; border-radius: 4px; background: #eee; }
`,
encapsulation: ViewEncapsulation.Emulated // the default; shown for clarity
})
export class BadgeComponent {}Here .label affects only spans inside app-badge. A .label class somewhere else on the page stays unstyled.
How Emulated Encapsulation Works
By default Angular uses ViewEncapsulation.Emulated. The compiler rewrites your CSS and template. Each element in the component's template gets an attribute such as _ngcontent-abc-c12, and the host element gets _nghost-abc-c12. Your selectors are then rewritten to require those attributes, so .label becomes .label[_ngcontent-abc-c12].
The :host Selector
The host element (<app-badge>) belongs to the parent's template, so plain selectors in your styles never reach it. Use :host to style it, and :host(.warning) to style it when the host has a class. :host-context(.dark) matches when any ancestor has the class dark.
Custom elements default to display: inline, so components often set :host { display: block; }.
Encapsulation Modes
| Mode | Behavior | Use when |
|---|---|---|
| `Emulated` (default) | Attribute-based scoping, global styles still apply | Almost always |
| `ShadowDom` | Real browser Shadow DOM, global styles blocked | Building isolated widgets or web components |
| `None` | Styles become global | Rarely; prefer a global stylesheet instead |
Common Errors
- Styling a child component's internals. The parent's
.labelrule won't match elements insideapp-badge, because those elements carry the child's attribute. Expose a CSS custom property instead, for examplebackground: var(--badge-bg, #eee), and set--badge-bgfrom the parent. - Projected content. Content passed in through
<ng-content>belongs to the parent, so the parent's styles apply to it and the child's do not. - Reaching for
::ng-deep. It turns off scoping for the rest of the selector and is deprecated. Use custom properties or global styles. - Setting
Noneto make styles work. Those rules then leak into the whole app and can clash with other components.
Exercise
A parent template contains <app-badge class="warning">Low stock</app-badge>, and the parent's styles include .label { color: red; }. Is the text red, and does the background turn orange?
Show answer
The text is not red. The parent's .label rule is rewritten with the parent's _ngcontent attribute, and the span inside the badge carries the badge's attribute instead. The background does turn orange, because :host(.warning) .label in the badge's own styles matches the host's warning class.