View Encapsulation Modes
Angular scopes component styles to prevent CSS leakage between components. This is called view encapsulation.
Emulated (Default)
Angular adds unique attribute selectors to component styles so they only apply to the component's template.
@Component({
selector: 'app-button',
template: `<button class="btn">Click</button>`,
styles: [`
.btn {
background: blue;
color: white;
padding: 8px 16px;
}
`],
// encapsulation: ViewEncapsulation.Emulated // default
})
export class ButtonComponent {}
The compiled CSS becomes:
.btn[_ngcontent-abc123] {
background: blue;
color: white;
padding: 8px 16px;
}
ShadowDom
Uses the browser's native Shadow DOM for true style isolation:
@Component({
selector: 'app-button',
template: `<button class="btn">Click</button>`,
styles: [`.btn { background: blue; }`],
encapsulation: ViewEncapsulation.ShadowDom,
})
export class ButtonComponent {}
Benefits:
- True style isolation (no leakage in or out)
- No global styles affect the component
Drawbacks:
- Some CSS features (like :host) behave differently
- Third-party libraries may not work correctly
None
No encapsulation — styles are global:
@Component({
selector: 'app-button',
template: `<button class="btn">Click</button>`,
styles: [`.btn { background: blue; }`],
encapsulation: ViewEncapsulation.None,
})
export class ButtonComponent {}
The CSS is applied globally without any scoping attributes. Use with caution.
Encapsulation Comparison
| Mode | Scoping | Performance | Use Case |
|---|---|---|---|
| Emulated | Attribute selectors | Good | Default — most components |
| ShadowDom | Native Shadow DOM | Best isolation | Widgets, design systems |
| None | None (global) | N/A | Global utility styles |
::ng-deep and Piercing Encapsulation
::ng-deep allows a component's styles to affect child components. It is discouraged but sometimes necessary.
::ng-deep Usage
@Component({
selector: 'app-parent',
template: `<app-child></app-child>`,
styles: [`
::ng-deep .child-class {
color: red;
}
`],
})
export class ParentComponent {}
This applies the style to .child-class inside app-child, piercing the child's encapsulation.
Why ::ng-deep Is Discouraged
- Breaks encapsulation — Parent styles leak into child components
- Hard to maintain — Styles are scattered across components
- Deprecated — The Angular team plans to remove it
Better Alternatives
1. Use CSS Custom Properties:
// Parent
@Component({
styles: [`
app-child {
--accent-color: red;
}
`],
})
// Child
@Component({
styles: [`
.child-class {
color: var(--accent-color, blue);
}
`],
})
2. Use Input Properties:
// Child accepts a theme input
@Input() theme: 'light' | 'dark' = 'light';
3. Use Global Styles:
/* styles.css */
app-child .child-class {
color: red;
}
:host and :host-context
@Component({
selector: 'app-card',
styles: [`
:host {
display: block;
border: 1px solid #ccc;
}
:host(.active) {
border-color: blue;
}
:host-context(.dark-theme) {
background: #333;
color: white;
}
`],
})
export class CardComponent {}
:host— targets the component's host element:host(.class)— targets the host when it has a specific class:host-context(.class)— targets the host when an ancestor has a class
Quiz
1. What is the default view encapsulation mode in Angular?
2. What does ::ng-deep do?
3. What does :host do in component styles?
Flashcards
Question
What is the default view encapsulation?
Click to reveal answer
Answer
Emulated — Angular adds unique attribute selectors to scope styles to the component.
Question
What does ::ng-deep do?
Click to reveal answer
Answer
Pierces encapsulation to style child components. Discouraged — use CSS custom properties instead.
Question
What does :host do?
Click to reveal answer
Answer
Targets the component's host element. Use :host(.class) for conditional styling.
Revision Notes
Key Takeaways
- 1. Emulated encapsulation (default) scopes styles with attribute selectors
- 2. ::ng-deep pierces encapsulation but is discouraged
- 3. Use CSS custom properties for parent-to-child styling
- 4. :host targets the component's own element
Interview Tips
- • Explain the three encapsulation modes
- • Be ready to describe why ::ng-deep is discouraged
- • Mention CSS custom properties as the modern alternative
Cheat Sheet
View Encapsulation Cheat Sheet
Modes:
Emulated(default) — attribute selectorsShadowDom— native Shadow DOMNone— global styles
Piercing encapsulation:
::ng-deep .child-class { } /* discouraged */
Host styling:
:host { }
:host(.active) { }
:host-context(.dark-theme) { }