الانتقال إلى المحتوى
FrontendAngular· 7 دقائق للقراءة

Component Styles Aren't Fully Isolated: View Encapsulation Explained

Emulated encapsulation keeps a component's styles in, not the page's styles out. Here's what crosses the line, and how to style a child without ::ng-deep.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Angular 22
محتويات المقال

A designer files a bug: the grey "New" badges in the product sidebar turn crimson, but only while the promotions carousel is on screen. Nobody touched the sidebar. The cause is one rule in the carousel's stylesheet, ::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

The belief: every Angular component is a sealed box. Its styles can't get out, outside styles can't get in, and when a parent really needs to style something inside a child, ::ng-deep is the standard tool for crossing the wall.

It's half right, which is why it sticks. Angular's docs do say a component's styles don't leak out and affect other components, and you can see it work: write .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

Every component has a mode, and the default, 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:

CSS badge.css
/* What you write in the Badge component */.badge { background: #e5e7eb; }/* Roughly what Angular emits (the attribute name varies) */.badge[_ngcontent-ng-c123] { background: #e5e7eb; }

The rewritten rule can only match elements that carry the attribute, and only the component's own template has it. That's the whole mechanism, and it works in one direction. It controls what your selectors can match. It does nothing to selectors written elsewhere.

What still gets in

A rule 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.

Inheritance doesn't stop either. In emulated mode the component's elements are ordinary children in the page's DOM, so inherited properties like 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

A parent's rewritten selectors match elements in the parent's template, and the child's host element, <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.

The better tool is the one the host-elements guide describes: CSS custom properties. The child reads a property with a default, and the parent sets it on the child's host element. Custom properties inherit, so the value reaches the span, and the child decides exactly which parts are configurable.

TypeScript product-card.ts
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 {}

The right version changes one badge: the one in this card's template. A binding such as [style.--badge-bg]="color()" on <app-badge> works the same way when the value is dynamic.

Real isolation is opt-in

If you need a stronger boundary, 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.

MythReality
Outside styles can't reach a componentGlobal rules match elements inside it
Inherited styles stop at the componentcolor, fonts and custom properties flow in
A parent can't style its child at allIt can style the child's host element
::ng-deep is the standard way inIt's discouraged and makes the rule global
Emulated mode is like Shadow DOMIt only rewrites your selectors

How to apply it in your project

  1. Search for the escape hatchesFind every ::ng-deep and ViewEncapsulation.None. As a first fix, put :host in front of each ::ng-deep so the rule can't reach outside its component.
  2. Shrink the global stylesheetKeep styles.css to resets, typography and design tokens. Element selectors like button or span hit every component, so turn the rest into classes or component styles.
  3. Give shared components a styling APIExpose the parts a parent may change as custom properties with defaults, plus inputs or host class bindings for variants, then replace the ::ng-deep rules with them.
  4. Check the winning ruleWhen 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-deep is strongly discouraged and turns a rule global; prefer a custom-property styling API.

📚 المصادر

  1. Styling components — Angular
  2. ViewEncapsulation — Angular
  3. Component host elements — Angular
  4. REMOVE_STYLES_ON_COMPONENT_DESTROY — Angular
  5. Inheritance — MDN

التعليقات (0)