Full answer
CSS is global in a web page, so a rule such as h2 { color: red } normally changes every h2. Angular avoids this with view encapsulation, which is set in the encapsulation option of @Component.
Emulated(default). Angular adds a generated attribute (like_ngcontent-abc-c12) to every element of the component and rewrites each selector to include it. Styles stay inside the component, and global styles still reach in. It is not a real shadow DOM.None. Styles are added to the page unchanged, so they are global. Useful for a theme file or when you must style third-party markup.ShadowDom. The component uses the browser's native shadow DOM. Strong isolation, and global styles do not enter. Fonts and global resets need extra care.
Selectors inside a component:
:hostselects the component's own element, for example:host { display: block; }. Custom elements aredisplay: inlineby default, so settingdisplay: blockhere is a common first step.:host(.active)selects the host when it has a class.:host-context(.dark)selects the host when an ancestor has the class (used for themes).
Styling a child from outside. The parent's scoped CSS cannot reach into a child's template. Good options, from best to worst:
- Expose CSS custom properties in the child (
color: var(--badge-color, navy)) and set them from the parent. - Give the child an input such as
variant. - Put the rule in a global stylesheet.
::ng-deeppierces encapsulation, but it is deprecated and should only appear in old code.
Remember that styles in styles or styleUrl are loaded with the component and removed from the page when the last instance is destroyed.
typescript
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-badge',
template: `<span class="label"><ng-content /></span>`,
styles: `
:host {
display: inline-block;
margin: 4px;
}
:host(.large) .label {
font-size: 1.5rem;
}
.label {
color: white;
padding: 2px 8px;
border-radius: 8px;
background: var(--badge-color, navy);
}
`,
})
export class BadgeComponent {}
@Component({
selector: 'app-theme-demo',
imports: [BadgeComponent],
encapsulation: ViewEncapsulation.Emulated,
template: `
<h2>Result</h2>
<app-badge>Passed</app-badge>
<app-badge class="large" style="--badge-color: darkred">Failed</app-badge>
`,
styles: `
/* Applies to this component's h2 only, not to the h2 elements of other components. */
h2 {
color: teal;
}
`,
})
export class ThemeDemoComponent {}
@Component({
selector: 'app-global-skin',
encapsulation: ViewEncapsulation.None,
template: `<p class="notice">This rule is global</p>`,
styles: `
.notice {
font-style: italic;
}
`,
})
export class GlobalSkinComponent {}Angular 20.3, strict template check passes. Set `--badge-color` on the parent or on any ancestor to change the badge colour.
Key points
- Emulated is the default and scopes styles by adding attributes. `None` is global. `ShadowDom` is native isolation.
- `:host` styles the component's own element. Set `display` there.
- CSS custom properties are the clean way for a parent to theme a child.
- `::ng-deep` is deprecated. Avoid it in new code.
Common mistakes
- Forgetting that a component's host element is `display: inline` and then wondering why width and margin do not work.
- Using `ViewEncapsulation.None` in many components, which brings back global CSS conflicts.
- Using `::ng-deep` to override a library instead of its supported theming variables.
Follow-ups
- How can you apply a global style to one specific component only?
- What changes in styling when you use `ShadowDom`?
- How does Angular Material expose theming without `::ng-deep`?
Tips
- Say that encapsulation is emulated by attributes. It shows you understand the mechanism.
- Offer CSS variables as the answer for child styling.
- Mention that `::ng-deep` is deprecated, and that you would only keep it in old code.
Evaluation rubric
- Names the three modes correctly.
- Uses :host and CSS custom properties.
- Knows ng-deep is deprecated.