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
}
stylesholds plain CSS. Since Angular 17 it may be one string, as above. The older array form,styles: [`p { color: blue; }`], still works, and it's the form the exercise uses.- In a CLI project the CSS is usually written in its own file:
styleUrl: './menu-item.css'. The Playground can't read separate files, so here it'sstyles. - MenuItem's four rules are covered one by one below. The first to try:
p { color: #c2410c; }, which makes paragraphs orange.
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; }
- Each component gets its own id: App
ng-c2838053730, MenuItemng-c3691344558. Angular calculates the number from the component's contents, so in your code it may differ. - Every element in a component's template gets that component's
_ngcontent-<id>stamp. The host element, which is the component's own tag (<app-menu-item>), gets_nghost-<id>. - Every CSS selector (the part of a rule before
{, such asp) is rewritten so it only matches elements with that stamp.pbecomesp[_ngcontent-ng-c3691344558]. <p>Open at 16:00</p>carries App's stamp, not MenuItem's, so MenuItem'sprule doesn't reach it. App's paragraph stays black.
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).
- The browser shows a host element like
<app-menu-item>asdisplay: inlineunless told otherwise. So the card's border and padding only sit right after:host { display: block; }. - App decides the class (
[class.sold-out]="true"on the second card's tag). MenuItem decides how it looks (:host(.sold-out), so the card fades). - Click Dark mode:
<main>gets thedarkclass, and both card titles turn amber.
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" />
[style.color]="..."fills one CSS property with the result of the expression. IfstockColor()returns'green', the DOM becomes<p style="color: green;">. There's also a form with a unit:[style.width.%]="percent"fillswidthwith the value ofpercentplus%.[class.sold-out]="condition"puts thesold-outclass on while the condition istrueand takes it off when it'sfalse.
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 |
- None switches the stamps off.
p { color: #c2410c; }becomes a rule for the whole page, so App's paragraph turns orange too: the styles leak out of their component.:hostisn't understood outside Shadow DOM, so the card borders vanish. - ShadowDom uses a real Shadow DOM. Rules from outside can't get in. Only inherited properties, such as text colour, pass through.
📌 ::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
- Writing the component's own tag in its styles.
app-menu-item { border: ... }inside MenuItem reaches nothing. Use:host. ViewEncapsulation.Noneto "fix" one rule. That component's CSS leaks into the whole app. Global styles belong insrc/styles.css.::ng-deepwithout:host. The rule applies to the whole page, not only under that component.- Exercise details. Add
styles: [`p { color: blue; }`]to the decorator and keep the starter's<p>Blue text</p>template. The grader checks two things: the colour the browser actually uses,rgb(0, 0, 255), and the wordstylesin your code. So<p style="color: blue">passes the colour check but still fails.
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.