Sub topic 11
::ng-deep and why it is deprecated
This chapter assumes you know how Emulated encapsulation scopes styles, how :host works, and how CSS custom properties cross component boundaries. Here you will learn what ::ng-deep does, why Angular deprecated it, and what to use instead.
What ::ng-deep does
Under Emulated encapsulation, Angular adds a generated attribute to each element in a component's template. It rewrites your selectors to require that attribute. A rule like .title becomes roughly .title[_ngcontent-abc]. Elements rendered by a child component or a third-party library carry a different attribute, so your rule never matches them.
::ng-deep tells the compiler to stop adding the attribute to everything after it in the selector. The rule can then match elements inside child components.
:host ::ng-deep .third-party-title {
font-weight: 700;
}Angular compiles this to roughly [_nghost-abc] .third-party-title. The host attribute stays on the left side, so the rule applies only to descendants of this component's host element. Without :host, the same rule would become global and affect every .third-party-title in the application.
Why it is deprecated
The Angular documentation marks ::ng-deep, /deep/, >>> and the :host-context piercing family as deprecated. The reasons are practical:
- The feature came from a proposed browser standard for piercing shadow boundaries. Browsers dropped that proposal, so there is no native equivalent to follow.
- It breaks the guarantee that a component's styles stay inside it. A rule in one component can change the look of another.
- It couples your CSS to the internal markup of a child component or library. A library update that renames a class silently breaks your styling.
- Specificity becomes hard to predict, because the rewritten selector no longer matches how the encapsulated styles are written.
The deprecation is a recommendation to avoid it. Angular has not announced a removal date, and existing code keeps working. Still, treat it as a last resort for new code.
The risk of leaking
A rule written without :host leaks into the whole application once the component is created. The leak also depends on which components have been loaded, so styles can change as the user navigates.
/* Risky: applies to every .mat-title in the app once this component loads */
::ng-deep .mat-title {
color: crimson;
}If you must use ::ng-deep, always anchor it with :host and keep the selector as narrow as you can.
Safer alternatives
Try these in order before reaching for ::ng-deep:
- CSS custom properties. If you control the child component, expose properties like
--card-backgroundand read them withvar()and a fallback. The parent sets them from outside, and the child decides what they affect. - Inputs and host bindings. Pass a variant or state to the child with an input. The child applies the right class itself.
- Global styles. For rules that should apply everywhere, such as a theme for a library's elements, use the global stylesheet. The rule's reach is then explicit.
- A dedicated
ViewEncapsulation.Nonewrapper component. This confines the unscoped rules to one small file, though they still leak globally, so prefix selectors carefully. - Library theming APIs. Many UI libraries publish their own custom properties or theming mixins. Use those first.
/* styles.css (global) */
app-card {
--card-background: #f8fafc;
--card-padding: 24px;
}When ::ng-deep is still justified
Sometimes you do not own the child markup, and the library offers no hook. A third-party widget that renders plain HTML inside your component is a common case. Then :host ::ng-deep is the narrowest tool available. Add a comment that names the library and version, so the next reader knows why it is there and what to check on upgrade.
Exercise
A component stylesheet contains ::ng-deep .item { color: red; } with no :host. What happens, and how do you fix it so the rule only affects descendants of this component?
Show answer
Angular drops the scoping attribute, so the rule becomes global and turns every .item in the application red once the component loads. Fix it by prefixing with :host, as in :host ::ng-deep .item { color: red; }. Better still, expose a CSS custom property in the child, if you own it, and set it from the parent.
Exercise
Name two reasons Angular recommends avoiding ::ng-deep, and one alternative that keeps encapsulation intact.
Show answer
It couples your CSS to a child's internal markup, and it breaks style isolation so one component can change another. (A third reason: the browser proposal it was based on was abandoned.) CSS custom properties with fallbacks let a parent theme a child without piercing encapsulation.