{"flag":true,"single":true,"pageTitle":"What is View Encapsulation in Angular | View Encapsulation | Angular 15+","post":{"id":63,"user_id":"1","slug":"what-is-view-encapsulation-in-angular-view-encapsulation-angular-15--ynqj","title":"What is View Encapsulation in Angular | View Encapsulation | Angular 15+","body":"<p>View Encapsulation in Angular is a technique used to control the scope of CSS styles defined within a component, preventing them from leaking out and affecting other elements or components in the application. Angular offers three encapsulation strategies, which determine how styles are applied and isolated:<\/p>\r\n<p>There are three types of View encapsulation in Angular<br><strong>ViewEncapsulation .None<\/strong> &gt;&gt; any class of css will applied to whole application ie text-success<\/p>\r\n<p>None: In this mode, Angular does not provide any style encapsulation. Styles defined within the component are treated as global styles and can affect other elements and components throughout the application. This mode can be useful if you intentionally want to create global styles or override existing styles.<\/p>\r\n<p><strong>ViewEncapsulation.Emulated&nbsp;<\/strong>&gt;&gt; each component has its own dom and own css<\/p>\r\n<p>Emulated (default): In this mode, Angular emulates shadow DOM by adding unique attributes to the host element and styles defined within the component, effectively scoping the styles to the component only. Although the styles are still in the global CSS, Angular ensures that they only affect the component they belong to.<\/p>\r\n<p><br><strong>ViewEncapsulation. ShadowDOM &gt;&gt;<\/strong><\/p>\r\n<p>In ViewEncapsulation.Emulated angular add a unique attribute to each component element<\/p>\r\n<p>In this mode, Angular uses the native Shadow DOM implementation provided by the browser to encapsulate styles. It attaches a shadow DOM to the host element, and styles are defined within the shadow DOM, isolating them from the rest of the application. This mode offers better style encapsulation but has limited browser support.<\/p>\r\n<p>First of all you need to import&nbsp;<\/p>\r\n<pre class=\"language-markup\"><code>import { ViewEncapsulation } from '@angular\/core';<\/code><\/pre>\r\n<p>We can set encapsulation&nbsp; in @component<\/p>\r\n<pre class=\"language-markup\"><code>@Component({\r\n  selector: 'app-filter',\r\n  templateUrl: '.\/filter.component.html',\r\n  styleUrls: ['.\/filter.component.css']\r\n  encapsulation: ViewEncapsulation.None\r\n})<\/code><\/pre>\r\n<p><strong>Example:<\/strong><\/p>\r\n<p><strong>1.Emulated (default):<br><\/strong><\/p>\r\n<pre class=\"language-markup\"><code>\/\/app.component.ts:\r\nimport { Component } from '@angular\/core';\r\n@Component({\r\n  selector: 'app-root',\r\n  template: `&lt;h1&gt;Emulated View Encapsulation&lt;\/h1&gt;`,\r\n  styles: [`h1 { color: red; }`],\r\n})\r\nexport class AppComponent { }<\/code><\/pre>\r\n<p>Output<\/p>\r\n<pre class=\"language-markup\"><code>&lt;!-- Angular adds a unique attribute to the element and the styles --&gt;\r\n&lt;app-root _ngcontent-c0&gt;\r\n  &lt;h1 _ngcontent-c0&gt;Emulated View Encapsulation&lt;\/h1&gt;\r\n&lt;\/app-root&gt;<\/code><\/pre>\r\n<p><strong>2.None:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>\/\/app.component.js\r\nimport { Component, ViewEncapsulation } from '@angular\/core';\r\n@Component({\r\n  selector: 'app-root',\r\n  template: `&lt;h1&gt;None View Encapsulation&lt;\/h1&gt;`,\r\n  styles: [`h1 { color: green; }`],\r\n  encapsulation: ViewEncapsulation.None,\r\n})\r\nexport class AppComponent { }\r\n<\/code><\/pre>\r\n<p>output:<\/p>\r\n<pre class=\"language-markup\"><code>&lt;!-- No unique attribute is added, styles are treated as global --&gt;\r\n&lt;app-root&gt;\r\n  &lt;h1&gt;None View Encapsulation&lt;\/h1&gt;\r\n&lt;\/app-root&gt;<\/code><\/pre>\r\n<p><strong>3.ShadowDom<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>import { Component, ViewEncapsulation } from '@angular\/core';\r\n@Component({\r\n  selector: 'app-root',\r\n  template: `&lt;h1&gt;Shadow DOM View Encapsulation&lt;\/h1&gt;`,\r\n  styles: [`h1 { color: blue; }`],\r\n  encapsulation: ViewEncapsulation.ShadowDom,\r\n})\r\nexport class AppComponent { }<\/code><\/pre>\r\n<p><strong>output:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;!-- A shadow root is created for the component --&gt;\r\n&lt;app-root&gt;\r\n  #shadow-root\r\n    &lt;style&gt;\r\n      h1 { color: blue; }\r\n    &lt;\/style&gt;\r\n    &lt;h1&gt;Shadow DOM View Encapsulation&lt;\/h1&gt;\r\n&lt;\/app-root&gt;<\/code><\/pre>","category_id":"13","is_private":"0","created_at":"2023-03-28T01:44:40.000000Z","updated_at":"2023-05-15T23:41:32.000000Z","category":{"id":13,"user_id":"1","name":"Angular","slug":"angular-rfjp","parent_id":"12","created_at":"2023-03-24T03:25:15.000000Z","updated_at":"2023-03-24T03:25:15.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"View Encapsulation in Angular is a technique used to control the scope of CSS styles defined within a component, preventing them from leakin - What is View Encapsulation in Angular | View Encapsulation | Angular 15+ (Updated: May 15, 2023) - Read more about What is View Encapsulation in Angular | View Encapsulation | Angular 15+ at my programming site [SITE]","categories":[]}