Sub topic 11
::ng-deep and why it is deprecated
This chapter assumes you know basic CSS selectors and specificity, can write a standalone Angular component with the @Component decorator, and have read the earlier chapters on Emulated encapsulation and :host. Here you will learn what ::ng-deep does, why Angular marks it deprecated, and which safer options to try first.
The problem `::ng-deep` solves
Under the default Emulated encapsulation, Angular rewrites each selector in a component's styles so it matches only elements in that component's own template. The rewrite works by adding a generated attribute to the selector and to the template's elements.
This isolation has a cost. Elements your component does not own never receive the attribute, so your styles cannot reach them. Typical cases are:
- Markup that a third-party library renders inside your component, such as a date picker or a chart.
- Elements created by a child component's template.
- Content that a library appends to the page, such as overlays and dialogs.
Suppose a library renders <h3 class="third-party-title"> inside your component. Your component's CSS cannot style it:
/* Does not match: the library's element has no scoping attribute */
.third-party-title {
font-weight: 700;
}::ng-deep is a pseudo-class-like marker that tells Angular's style compiler to stop adding the scoping attribute to the part of the selector after it.
How the rewrite works
Angular compiles the selector and adds a content attribute to each compound selector before the marker. The attribute name is generated per component, for example _ngcontent-ng-c123456. The examples below use a simplified name so the output is readable.
| You write | Angular emits (simplified) | What it matches |
|---|---|---|
| `.title` | `.title[_ngcontent-abc]` | `.title` elements in this component's template only |
| `:host .title` | `[_nghost-abc] .title[_ngcontent-abc]` | `.title` elements in this template, inside the host |
| `::ng-deep .title` | `.title` | Every `.title` in the whole document |
| `:host ::ng-deep .title` | `[_nghost-abc] .title` | Any `.title` that is a descendant of this component's host |
The third row is the main hazard. A bare ::ng-deep rule is effectively a global style that happens to live in a component file. It also only exists while that component is created, because Angular adds the style element when the component first renders and can remove it later. Its effect on the rest of the page therefore depends on which components are currently on screen.
The least-leaky form
If you must use ::ng-deep, always put :host in front of it. That anchors the rule to the component's own host element, so it can only affect descendants of this component.
:host ::ng-deep .third-party-title {
font-weight: 700;
}A component using it looks like this:
import { Component } from '@angular/core';
@Component({
selector: 'app-report',
template: `
<section>
<h2>Quarterly report</h2>
<!-- A third-party chart component renders .third-party-title inside -->
<vendor-chart />
</section>
`,
styles: `
:host ::ng-deep .third-party-title {
font-weight: 700;
}
`,
})
export class ReportComponent {}The rule reaches the chart's title, but only while that chart is inside app-report. Another page that uses vendor-chart outside this component keeps its original look.
Why it is deprecated
The Angular documentation describes the ::ng-deep pseudo-class as deprecated. The deprecation comes from the history of the feature.
::ng-deep was an Angular-specific replacement for the /deep/ and >>> combinators. These in turn came from the proposed "shadow-piercing" combinators of the original Shadow DOM specification. Browsers dropped that specification, and the combinators were removed from Chrome. Angular kept its own implementation so that existing applications would keep working. The Angular team marks it as deprecated because it no longer follows any standard, and it recommends that you avoid it in new code.
Three practical problems remain:
- It breaks the encapsulation contract. The reason to use component styles is that each component's CSS is predictable and local. A deep rule lets one component restyle another component's internals.
- It couples you to internal markup. The selector depends on class names and element structure inside a library or child component. A library update can rename
.third-party-titleand silently break your rule, because there is no compile-time check. - It has no equivalent under ShadowDom. With
ViewEncapsulation.ShadowDom, the browser's shadow boundary decides what styles can cross, and Angular's attribute rewriting is not involved. A::ng-deeprule in a ShadowDom component does not pierce the boundary.
Deprecated does not mean removed. Angular has not announced a removal date, and many libraries and applications still use it. Treat it as supported but discouraged: acceptable for a contained workaround, and a poor choice as a default.
Safer alternatives to try first
Work through these options in order before reaching for ::ng-deep.
1. CSS custom properties
If you control the child component, or the library documents custom properties, set them from the parent. Custom properties inherit through the DOM, so they cross encapsulation boundaries without any piercing.
/* styles.css (global) */
app-card {
--card-background: #f8fafc;
--card-padding: 24px;
}The child reads them with fallbacks:
import { Component } from '@angular/core';
@Component({
selector: 'app-card',
template: `<ng-content />`,
styles: `
:host {
display: block;
padding: var(--card-padding, 16px);
background: var(--card-background, white);
}
`,
})
export class CardComponent {}This is the preferred way to expose a styling API. The child decides which values can change, and parents cannot reach anything else.
2. Inputs and host bindings
When a style depends on state, pass that state through an input and let the child apply its own classes. The parent never needs to know the child's internal markup.
import { Component, input } from '@angular/core';
@Component({
selector: 'app-alert',
template: `<ng-content />`,
styles: `
:host { display: block; padding: 8px; }
:host(.severe) { background: #fee2e2; }
`,
host: {
'[class.severe]': 'severity() === "severe"',
},
})
export class AlertComponent {
severity = input<'info' | 'severe'>('info');
}A parent then writes <app-alert severity="severe"> and the child restyles itself.
3. Styling the host from outside
The host element of any child component belongs to your template, so you can give it a class and style it from your own component styles. This works under Emulated encapsulation, because Angular adds your component's scoping attribute to the child's host element.
import { Component } from '@angular/core';
@Component({
selector: 'app-dashboard',
template: `<app-card class="wide">Totals</app-card>`,
styles: `
.wide { width: 100%; }
`,
})
export class DashboardComponent {}This only reaches the host element. Anything deeper inside app-card is still out of reach.
4. A global stylesheet
For a rule that really applies to third-party markup across the whole app, put it in styles.css or another global file. A global rule is honest about its scope, and anyone looking for the style can find it in one place. This is also the right place to style overlay content that a library attaches to document.body, outside your component entirely.
5. A different encapsulation mode
If a single component needs to be styled freely from outside, ViewEncapsulation.None makes its styles global on purpose. It is a broader tool than ::ng-deep, so use it only when the component is meant to behave that way.
Decision guide
| Situation | Preferred approach |
|---|---|
| Changing a color or spacing in a child you own | CSS custom properties |
| Changing a child's look based on state | Input plus host binding |
| Positioning or sizing a child's host element | Class on the host, styled in the parent |
| Restyling library markup inside one component | `:host ::ng-deep`, with a comment naming the library |
| Styling overlays attached to `body` | Global stylesheet |
| Styling every instance of a library widget | Global stylesheet |
| Component is a deliberately unstyled theme target | `ViewEncapsulation.None` |
Common mistakes
- Using a bare
::ng-deep. It leaks into the whole document. Always prefix it with:host. - Expecting it to work with ShadowDom. The shadow root is a browser boundary that Angular's compiler cannot rewrite around.
- Overriding with higher specificity and nothing else.
::ng-deepremoves scoping but does not change specificity. If a library's rule wins, you may still need a more specific selector. - Relying on library class names without a note. Add a comment with the library and version so the next upgrade is easier to check.
- Forgetting that library styles may load later. Order of stylesheets still affects which rule wins when specificity is equal.
Exercise
A component has the rule ::ng-deep .title { color: red; }. A different page of the same application, which never renders this component, also has .title elements. Once the user visits the component's page and then navigates away, can the rule still affect other .title elements, and what is the one change that limits the rule to this component's descendants?
Show answer
Yes. A bare ::ng-deep rule has no scoping attribute, so it matches every .title in the document while the component's styles are attached, and this can include elements on other pages that are rendered at the same time. Angular does not guarantee when component styles are removed, so you cannot rely on them disappearing right after navigation. The fix is to prefix the rule with :host, giving :host ::ng-deep .title { color: red; }, which restricts it to descendants of this component's host element.
Exercise
A parent component wants a child app-card to show a different background color. The child's author can change the child. Name an approach that avoids ::ng-deep and write the parent-side rule.
Show answer
Use a CSS custom property. The child reads var(--card-background, white) in its styles, and the parent or a global stylesheet sets app-card { --card-background: #f8fafc; }. Custom properties inherit through the DOM, so the value reaches the child's encapsulated styles without piercing encapsulation.