{ "id": "guide/view-encapsulation", "title": "View encapsulation", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n

View encapsulationlink

\n

In Angular, component CSS styles are encapsulated into the component's view and don't\naffect the rest of the application.

\n

To control how this encapsulation happens on a per\ncomponent basis, you can set the view encapsulation mode in the component metadata.\nChoose from the following modes:

\n\n

To set the component's encapsulation mode, use the encapsulation property in the component metadata:

\n\n// warning: few browsers support shadow DOM encapsulation at this time\nencapsulation: ViewEncapsulation.ShadowDom\n\n\n

ShadowDom view encapsulation only works on browsers that have native support\nfor shadow DOM (see Shadow DOM v1 on the\nCan I use site). The support is still limited,\nwhich is why Emulated view encapsulation is the default mode and recommended\nin most cases.

\n\n

Inspecting generated CSSlink

\n

When using emulated view encapsulation, Angular preprocesses\nall component styles so that they approximate the standard shadow CSS scoping rules.

\n

In the DOM of a running Angular application with emulated view\nencapsulation enabled, each DOM element has some extra attributes\nattached to it:

\n\n <hero-details _nghost-pmm-5>\n <h2 _ngcontent-pmm-5>Mister Fantastic</h2>\n <hero-team _ngcontent-pmm-5 _nghost-pmm-6>\n <h3 _ngcontent-pmm-6>Team</h3>\n </hero-team>\n </hero-detail>\n\n\n

There are two kinds of generated attributes:

\n\n

The exact values of these attributes aren't important. They are automatically\ngenerated and you should never refer to them in application code. But they are targeted\nby the generated component styles, which are in the <head> section of the DOM:

\n\n [_nghost-pmm-5] {\n display: block;\n border: 1px solid black;\n }\n\n h3[_ngcontent-pmm-6] {\n background-color: white;\n border: 1px solid #777;\n }\n\n

These styles are post-processed so that each selector is augmented\nwith _nghost or _ngcontent attribute selectors.\nThese extra selectors enable the scoping rules described in this page.

\n\n \n
\n\n\n" }