Angular - View Encapsulation
Angular - View Encapsulation
component's host element so that they don't affect the rest of the
application.
component basis.
CSS.
the component's styles so that they are only applied to the component's
view.
_nghost-pmm-5
_ngcontent-pmm-5 Mister Fantastic
_ngcontent-pmm-5 _nghost-pmm-6
_ngcontent-pmm-6 Team
ATTRIBUTES DETAILS
of Angular. They are automatically created and you should never refer to
them in application code.
They are targeted by the created component styles, which are injected in
[_nghost-pmm-5] {
display: block;
border: 1px solid black;
}
h3[_ngcontent-pmm-6] {
background-color: white;
border: 1px solid #777;
}
ViewEncapsulation.ShadowDom component.
be available.
Examples
This section shows examples of how the styling of components with
No encapsulation
elements red.
src/app/no-encapsulation.component.ts
@Component({
standalone: ,
selector: 'app-no-encapsulation',
: `
<h2>None<∕h2>
<div class="none-message">No encapsulation<∕div>
`,
styles: ['h2, .none-message { color: red; }'],
encapsulation: . ,
})
{}
Angular adds the styles for this component as global styles to the
As already mentioned, Angular also adds the styles to all shadow DOM
Emulated encapsulation
elements green.
src/app/emulated-encapsulation.component.ts
@Component({
standalone: ,
selector: 'app-emulated-encapsulation',
: `
<h2>Emulated<∕h2>
<div class="emulated-message">Emulated
encapsulation<∕div>
<app-no-encapsulation><∕app-no-encapsulation>
`,
styles: ['h2, .emulated-message { color: green;
}'],
encapsulation: . ,
imports: [ ],
})
{}
styles.
Only the elements directly within this component's template are going to
elements blue.
src/app/shadow-dom-encapsulation.component.ts
@Component({
standalone: ,
selector: 'app-shadow-dom-encapsulation',
: `
<h2>ShadowDom<∕h2>
<div class="shadow-message">Shadow DOM
encapsulation<∕div>
<app-emulated-encapsulation><∕app-emulated-
encapsulation>
<app-no-encapsulation><∕app-no-encapsulation>
`,
styles: ['h2, .shadow-message { color: blue; }'],
encapsulation: . ,
imports: [ ,
],
})
{}
Angular adds styles for this component only to the shadow DOM host,
NoEncapsulationComponent and
EmulatedEncapsulationComponent .
EmulatedEncapsulationComponent .
the shadow host after the global styles, the h2 style overrides the style