Component styles and encapsulation keep each component's look separate. This stops styles from mixing up and breaking the page design.
Component styles and encapsulation in Angular
import { Component, ViewEncapsulation } from '@angular/core'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'], encapsulation: ViewEncapsulation.Emulated // or None, ShadowDom }) export class ExampleComponent {}
encapsulation controls how styles apply to the component.
Common options are Emulated (default), None, and ShadowDom.
@Component({
selector: 'app-box',
template: `<div class='box'>Box</div>`,
styles: [`.box { color: blue; }`],
encapsulation: ViewEncapsulation.Emulated
})
export class BoxComponent {}p tags.@Component({
selector: 'app-global-style',
template: `<p>Global style</p>`,
styles: [`p { color: red; }`],
encapsulation: ViewEncapsulation.None
})
export class GlobalStyleComponent {}@Component({
selector: 'app-shadow',
template: `<button>Shadow DOM</button>`,
styles: [`button { background: yellow; }`],
encapsulation: ViewEncapsulation.ShadowDom
})
export class ShadowComponent {}This component styles the paragraph text green and bold. Because of Emulated encapsulation, these styles only affect this component's paragraph, not others on the page.
import { Component, ViewEncapsulation } from '@angular/core'; @Component({ selector: 'app-style-demo', template: ` <h2>Encapsulation Demo</h2> <p class="text">This text is styled inside the component.</p> `, styles: [ `.text { color: green; font-weight: bold; }` ], encapsulation: ViewEncapsulation.Emulated }) export class StyleDemoComponent {}
Emulated is the default and works well for most cases.
Use None if you want styles to affect the whole app, but be careful to avoid conflicts.
ShadowDom requires browser support but gives the strongest style isolation.
Component styles keep each part of your app looking right without mixing styles.
Encapsulation controls how styles apply: Emulated, None, or ShadowDom.
Choose the right encapsulation to balance style isolation and app needs.
Practice
ViewEncapsulation.Emulated do in Angular component styles?Solution
Step 1: Understand Emulated encapsulation
Emulated encapsulation scopes styles by adding unique attributes to elements, so styles apply only inside the component.Step 2: Compare with other modes
Unlike None (global styles) or ShadowDom (native Shadow DOM), Emulated simulates encapsulation without browser Shadow DOM.Final Answer:
It scopes styles to the component using generated attributes, preventing style leaks. -> Option CQuick Check:
Emulated = Scoped styles with attributes [OK]
- Confusing Emulated with None or ShadowDom
- Thinking Emulated disables styles
- Assuming Emulated uses native Shadow DOM
Solution
Step 1: Identify the correct encapsulation for no style isolation
ViewEncapsulation.None disables style encapsulation, making styles global.Step 2: Check other options
Emulated scopes styles, Native is deprecated, ShadowDom uses native Shadow DOM, so only None disables encapsulation.Final Answer:
@Component({ encapsulation: ViewEncapsulation.None }) -> Option BQuick Check:
No encapsulation = ViewEncapsulation.None [OK]
- Using Emulated or ShadowDom instead of None
- Confusing Native with None (Native is deprecated)
- Missing the encapsulation property entirely
@Component({
selector: 'app-box',
template: `Hello`,
styles: [`.box { color: red; }`],
encapsulation: ViewEncapsulation.None
})
export class BoxComponent {}What will be the color of all elements with class
box in the entire app?Solution
Step 1: Understand ViewEncapsulation.None effect
None disables style encapsulation, so styles apply globally to all matching elements.Step 2: Apply to the .box class
The style `.box { color: red; }` will affect every element with class 'box' in the entire app, not just inside this component.Final Answer:
Allboxelements in the app will be red. -> Option AQuick Check:
None encapsulation = global styles [OK]
- Assuming styles stay inside component with None
- Confusing ShadowDom encapsulation effects
- Thinking Emulated behaves like None
@Component({
selector: 'app-test',
template: `Test
`,
styles: [`p { font-weight: bold; }`],
encapsulation: ViewEncapsulation.ShadowDom
})
export class TestComponent {}But the
p text is not bold in the browser. What is the likely cause?Solution
Step 1: Understand ShadowDom encapsulation
ShadowDom encapsulates styles inside the browser's native Shadow DOM, isolating them from the main document.Step 2: Inspecting styles outside Shadow DOM
If you inspect the page outside the Shadow DOM, you won't see the styles applied because they are inside the component's shadow root.Final Answer:
The styles are scoped inside Shadow DOM, but you are inspecting outside it. -> Option AQuick Check:
ShadowDom styles live inside shadow root, not visible outside [OK]
- Assuming ShadowDom styles apply globally
- Not checking if component is used in HTML
- Thinking browser lacks Shadow DOM support
Solution
Step 1: Identify the goal for style isolation and native features
You want true style isolation and to use the browser's native Shadow DOM capabilities.Step 2: Match encapsulation mode to goal
ViewEncapsulation.ShadowDom uses native Shadow DOM, providing real isolation and native browser support.Step 3: Evaluate other options
None applies styles globally (not isolated), Emulated simulates but does not use native Shadow DOM, and default does not guarantee native isolation.Final Answer:
Use ViewEncapsulation.ShadowDom to leverage native Shadow DOM for true style isolation. -> Option DQuick Check:
Native isolation = ShadowDom encapsulation [OK]
- Choosing None and expecting isolation
- Confusing Emulated with native Shadow DOM
- Assuming Angular isolates styles without setting encapsulation
Ready to prove it?
Pass the test and this pattern moves unknown
