css – How and where to use ::ng-deep?

css – How and where to use ::ng-deep?

Usually /deep/ “shadow-piercing” combinator can be used to force a style down to child components. This selector had an alias >>> and now has another one called ::ng-deep.

since /deep/ combinator has been deprecated, it is recommended to use ::ng-deep

For example:

<div class=overview tab-pane id=overview role=tabpanel [innerHTML]=project?.getContent( DETAILS)></div>

and css

.overview {
    ::ng-deep {
        p {
            &:last-child {
                margin-bottom: 0;

it will be applied to child components


::ng-deep, >>> and /deep/ disable view encapsulation for specific CSS rules, in other words, it gives you access to DOM elements, which are not in your components HTML. For example, if youre using Angular Material (or any other third-party library like this), some generated elements are outside of your components area (such as dialog) and you cant access those elements directly or using a regular CSS way. If you want to change the styles of those elements, you can use one of those three things, for example:

::ng-deep .mat-dialog {
  /* styles here */

For now Angular team recommends making deep manipulations only with EMULATED view encapsulation.


deep manipulations are actually deprecated too, BUT it stills working for now, because Angular does pre-processing support (dont rush to refuse ::ng-deep today, take a look at deprecation practices first).

Anyway, before following this way, I recommend you to take a look at disabling view encapsulation approach (which is not ideal too, it allows your styles to leak into other components), but in some cases, its a better way. If you decided to disable view encapsulation, its strongly recommended to use specific classes to avoid CSS rules intersection, and finally, avoid a mess in your stylesheets. Its really easy to disable right in the components .ts file:

  selector: ,
  template: ,
  styles: [],
  encapsulation: ViewEncapsulation.None  // Use to disable CSS Encapsulation for this component

You can find more info about the view encapsulation in this article.

css – How and where to use ::ng-deep?

I would emphasize the importance of limiting the ::ng-deep to only children of a component by requiring the parent to be an encapsulated css class.

For this to work its important to use the ::ng-deep after the parent, not before otherwise it would apply to all the classes with the same name the moment the component is loaded.

Using the :host keyword before ::ng-deep will handle this automatically:

:host ::ng-deep .mat-checkbox-layout

Alternatively you can achieve the same behavior by adding a component scoped CSS class before the ::ng-deep keyword:

.my-component ::ng-deep .mat-checkbox-layout {
    background-color: aqua;

Component template:

<h1 class=my-component>
    <mat-checkbox ....></mat-checkbox>

Resulting (Angular generated) css will then include the uniquely generated name and apply only to its own component instance:

.my-component[_ngcontent-c1] .mat-checkbox-layout {
    background-color: aqua;

Leave a Reply

Your email address will not be published.