
Component Styles Aren't Fully Isolated: View Encapsulation Explained
- كاتب موثّق
- Angular 22
محتويات المقال
::ng-deep .badge { background: crimson; }, written to recolor the single badge inside the carousel. Its author believed component styles are sealed, so the rule could only reach that child. The same bug report adds that the badge text uses the wrong font everywhere, which comes from a span rule in styles.css that nobody thought could get in either.
The common misconception
::ng-deep is the standard tool for crossing the wall.
.title { color: red } in one component and the .title in its neighbor stays black. Try styling a child's inner span from the parent and nothing happens either, which feels like proof that the wall goes both ways. Then a search for "style child component Angular" turns up years of answers that recommend ::ng-deep, so it looks like the sanctioned escape hatch.
What actually happens
Emulated encapsulation rewrites selectors
ViewEncapsulation.Emulated, builds no wall at all. Angular generates a unique attribute, adds it to every element in the component's template, and appends it to every selector in the component's styles:
/* What you write in the Badge component */.badge { background: #e5e7eb; }/* Roughly what Angular emits (the attribute name varies) */.badge[_ngcontent-ng-c123] { background: #e5e7eb; }What still gets in
styles.css such as span { font-family: serif } has no attribute in it, so it matches every span on the page, including the ones inside your components. The styling guide says it plainly: global styles may still affect elements inside a component with emulated encapsulation.
color, font-family and line-height flow down from whatever wraps them, and so do . Stylesheets a component pulls in through a <link> element or an @import are treated as external and aren't scoped at all. And when an outside rule and a component rule collide, Angular doesn't promise that the component wins; the guide notes it assumes both have the same specificity.
Where a parent's styles stop
<app-badge>, is one of them. So the parent can style the host: its margin, its width, its custom properties. The span inside belongs to the child's template and carries the child's attribute, so the parent's selectors can't match it. Projected content follows the same rule: elements passed through are styled by the component that wrote them, not by the one that displays them.
What ::ng-deep really does
::ng-deep tells Angular to stop adding the encapsulation attribute after that point in the selector. Written at the start, ::ng-deep .badge matches every .badge in the application, which makes it a global style. With :host in front, :host ::ng-deep .badge is limited to descendants of the component's host element. The Angular team strongly discourages any new use of ::ng-deep and keeps it only for backwards compatibility.
span, and the child decides exactly which parts are configurable.
import { Component, input } from '@angular/core';@Component({ selector: 'app-badge', template: `<span class="badge">{{ label() }}</span>`, styles: `.badge { background: #e5e7eb; }`,})export class Badge { readonly label = input('');}@Component({ selector: 'app-product-card', imports: [Badge], template: `<app-badge label="Sale" />`, // Global: recolors every .badge in the app styles: `::ng-deep .badge { background: crimson; }`,})export class ProductCard {}import { Component, input } from '@angular/core';@Component({ selector: 'app-badge', template: `<span class="badge">{{ label() }}</span>`, // The styling API: a custom property with a default styles: `.badge { background: var(--badge-bg, #e5e7eb); }`,})export class Badge { readonly label = input('');}@Component({ selector: 'app-product-card', imports: [Badge], template: `<app-badge label="Sale" />`, // Scoped to this card's own <app-badge> host element styles: `app-badge { --badge-bg: crimson; }`,})export class ProductCard {}[style.--badge-bg]="color()" on <app-badge> works the same way when the value is dynamic.
Real isolation is opt-in
ViewEncapsulation.ShadowDom renders the component into a real shadow root, so its styles can't affect anything outside it. Only the experimental ExperimentalIsolatedShadowDom mode also guarantees that global styles can't affect the elements inside. Both change more than styling: Shadow DOM affects event propagation, <slot> behavior and what DevTools shows, so treat them as an architectural choice, not a CSS fix. ViewEncapsulation.None goes the other way and makes all of a component's styles global.
color, fonts and custom properties flow in | |
::ng-deep is the standard way in | |
How to apply it in your project
Search for the escape hatches Find every ::ng-deepandViewEncapsulation.None. As a first fix, put:hostin front of each::ng-deepso the rule can't reach outside its component.Shrink the global stylesheet Keep styles.cssto resets, typography and design tokens. Element selectors likebuttonorspanhit every component, so turn the rest into classes or component styles.Give shared components a styling API Expose the parts a parent may change as custom properties with defaults, plus inputs or host class bindings for variants, then replace the ::ng-deeprules with them.Check the winning rule When a style looks wrong, open the Computed pane in DevTools, find the property and follow it to the rule that set it. It shows whether it came from your component, a global file or inheritance.
Key takeaways
Emulated encapsulation rewrites your selectors so they can't match outside your template; it doesn't block anything. Global stylesheets, inherited properties and custom properties still reach inside every component. A parent can style its child's host element and set custom properties on it, but not the child's inner elements. ::ng-deepis strongly discouraged and turns a rule global; prefer a custom-property styling API.
التعليقات (0)