Styling in Angular

CSS in a component's styles is encapsulated: it only applies to that component. With ViewEncapsulation.Emulated (the default), Angular stamps its template's…

In an ordinary web project, one rule like p { color: orange; } in a CSS file colours every paragraph on the page, including paragraphs you didn't mean. The bigger the project, the more often a rule meant for one part also reaches another part.

Angular components don't have that problem. Each component can carry its own CSS through the styles property of its decorator. By default, that CSS only applies to that component. The word for it: its styles are encapsulated (kept inside their component). Picture the food court where Jali's pancake cart sells. Every cart stamps all its plates with its own stamp. If Jali says "paint the plates stamped Jali orange", the next cart's plates don't change, even though they're plates too. In Angular, the cart is a component, the plates are the elements in its template, and the stamp is a special attribute Angular puts on each of those elements. Then every CSS rule of the component is rewritten so it only reaches elements with that component's stamp.

styles: a component's own CSS

In this code, MenuItem has four CSS rules in styles. App shows two MenuItems, each as a card with a border:

import { Component, Input } from '@angular/core'

@Component({
  selector: 'app-menu-item',
  standalone: true,
  template: `
    <h3>{{ name }}</h3>
    <p>Stock: {{ stock }}</p>
  `,
  styles: `
    :host { display: block; margin-top: 8px; padding: 10px 12px; border: 1px solid #ddd; border-radius: 12px; }
    :host(.sold-out) { opacity: 0.5; }
    :host-context(.dark) h3 { color: #fbbf24; }
    p { color: #c2410c; }
  `,
})
export class MenuItem {
  @Input() name = ''
  @Input() stock = 0
}

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [MenuItem],
  template: `
    <main [class.dark]="dark">
      <h1>Jali's Pancake Stall</h1>
      <p>Open at 16:00</p>
      <button (click)="dark = !dark">Dark mode</button>
      <app-menu-item name="Special Egg Pancake" [stock]="6" />
      <app-menu-item name="Pandan Pancake" [stock]="0" [class.sold-out]="true" />
    </main>
  `,
  styles: `
    main { padding: 12px; border-radius: 12px; }
    .dark { background: #1f2937; color: #f9fafb; }
  `,
})
export class App {
  dark = false
}

Behind the scenes: the _ngcontent stamp

Open DevTools on the Playground, then the Elements tab. This is the DOM Angular 22 (the Playground's version) builds from the code above. The ng-version attribute is left out to keep it short:

<app-root _nghost-ng-c2838053730="">
  <main _ngcontent-ng-c2838053730="">
    <p _ngcontent-ng-c2838053730="">Open at 16:00</p>
    <app-menu-item _ngcontent-ng-c2838053730="" _nghost-ng-c3691344558="">
      <h3 _ngcontent-ng-c3691344558="">Special Egg Pancake</h3>
      <p _ngcontent-ng-c3691344558="">Stock: 6</p>
    </app-menu-item>

And this is MenuItem's p rule as Angular writes it into <head>:

p[_ngcontent-ng-c3691344558] { color: #c2410c; }

This mode is called ViewEncapsulation.Emulated, Angular's default. Emulated means imitated: Angular imitates Shadow DOM without a real Shadow DOM. Shadow DOM is a browser feature: an element gets a hidden DOM of its own (a shadow root), and CSS rules from outside don't apply inside it. In a CLI project the rewrite happens at build time. The ng build output stores p[_ngcontent-%COMP%]{color:#c2410c}, and %COMP% is replaced with the component's id when the app runs. A component with no styles at all gets no stamps.

:host and :host-context

The host element <app-menu-item> is written in App's template. So it carries App's _ngcontent and MenuItem's _nghost, but not MenuItem's _ngcontent. That's why an app-menu-item { ... } rule inside MenuItem, rewritten as app-menu-item[_ngcontent-ng-c3691344558], never matches. To style the host element from inside MenuItem, use :host:

Written in MenuItem Rewritten by Angular as Meaning
:host { display: block; ... } [_nghost-ng-c3691344558] { ... } the <app-menu-item> element itself
:host(.sold-out) { opacity: 0.5; } .sold-out[_nghost-ng-c3691344558] { ... } a host that has the sold-out class

:host-context(.dark) h3 means: an h3 in MenuItem, when its host or one of its ancestors (elements that wrap it) has the dark class. Angular rewrites it as two selectors at once: .dark[_nghost-ng-c3691344558] h3[_ngcontent-ng-c3691344558] (the host itself has the dark class) and .dark [_nghost-ng-c3691344558] h3[_ngcontent-ng-c3691344558] (one of its ancestors has it).

Binding style and class

For computed values, bind the style and the class straight in the template, as in the Angular Template Syntax lesson. In this example m is one menu item, for example inside a @for:

<p [style.color]="stockColor(m.stock)">{{ m.stock }} left</p>
<app-menu-item [name]="m.name" [stock]="m.stock" [class.sold-out]="m.stock === 0" />

ViewEncapsulation.None and ShadowDom

ViewEncapsulation has two other modes. You pick the mode with encapsulation in the decorator:

import { ViewEncapsulation } from '@angular/core'

@Component({
  selector: 'app-menu-item',
  standalone: true,
  encapsulation: ViewEncapsulation.None,
  // template and styles as before
})

This is what each mode does to the MenuItem and App above:

encapsulation MenuItem's CSS Open at 16:00 (App) Card border (:host)
Emulated (default) rewritten with stamps black there
None goes into <head> as written, applies globally orange gone
ShadowDom inside each card's shadow root black there

📌 ::ng-deep, the deprecated old way

This rule is written in App's styles:

/* App's styles */
:host ::ng-deep h3 { text-decoration: underline; }

Angular rewrites it as [_nghost-ng-c2838053730] h3: App's host stamp in front, and no stamp on h3. So every h3 inside App is reached, including the titles inside MenuItem, and those titles get underlined too. ::ng-deep is deprecated and only kept for old code. Without :host in front, ::ng-deep h3 even becomes a global rule for the whole page. If App really needs to change how MenuItem looks, ask MenuItem to offer an @Input() or a CSS variable (a CSS value that can be set from outside), for example color: var(--title-color, inherit), which uses inherit when --title-color isn't set.

⚠️ CSS that misses, or leaks everywhere

TL;DR: CSS in a component's styles is encapsulated: it only applies to that component. With ViewEncapsulation.Emulated (the default), Angular stamps its template's elements with _ngcontent-<id>, an attribute that belongs to that component. Then Angular rewrites p as p[_ngcontent-<id>], so other components' paragraphs don't change. :host styles the host element, which is the component's own tag, and :host(.sold-out) styles a host that has a given class. :host-context(.dark) applies when one of the host's ancestors (elements that wrap it) has that class. [style.color]="..." and [class.sold-out]="..." bind a style and a class to an expression. ViewEncapsulation.None makes the CSS global, so it leaks into the whole page. ShadowDom uses a real Shadow DOM (a browser feature that keeps CSS from outside an element out). ::ng-deep is deprecated.

⚡ styles: a component's own CSS

Write:

@Component({
  selector: 'app-menu-item',
  template: `<h3>{{ name }}</h3> <p>Stock: {{ stock }}</p>`,
  styles: `p { color: #c2410c; }`,
})
Guess first: #c2410c is an orange colour. The App component, which shows this component (MenuItem), also has <p>Open at 16:00</p> in its own template. Does that paragraph turn orange too?

No, it stays black. Angular gives every element in MenuItem's template a stamp, the _ngcontent-<id> attribute, then rewrites p as p[_ngcontent-<id>]. App's paragraph doesn't have that stamp.

Meaning: CSS in styles is encapsulated: it only applies to that component.

In the exercise, add styles: [`p { color: blue; }`] and leave <p>Blue text</p> as it is. A style attribute straight on the <p> doesn't pass.

⚡ :host for the host element

Write:

:host { display: block; border: 1px solid #ddd; }
:host(.sold-out) { opacity: 0.5; }

Meaning: The host element is the component's own tag, here <app-menu-item>. :host styles that element from inside MenuItem, and :host(.sold-out) styles a host that has the sold-out class. Writing app-menu-item { ... } inside MenuItem reaches nothing, because that tag is written in App's template. App puts the class on, for example with [class.sold-out]="m.stock === 0" (m.stock is that menu item's stock). MenuItem decides how it looks.

⚡ None makes the CSS leak

Write:

encapsulation: ViewEncapsulation.None,

Meaning: encapsulation picks how Angular keeps a component's CSS inside it. ViewEncapsulation.None switches the stamps off, so MenuItem's p { color: #c2410c; } applies to the whole page, and App's paragraph turns orange too. Another mode, ShadowDom, uses a real Shadow DOM (a browser feature that keeps CSS from outside an element out). ::ng-deep, an old way to get past the stamps, is deprecated.

What you will learn