10

I want to change the styles of ng bootstrap pagination component and using /deep/ links in an Angular 6 app. The following code works fine but the console is showing a warning that the code is deprecated.

So, how should I change it to get rid of the warning?

Here is the CSS code I am currently using:

ngb-pagination /deep/ .page-item.disabled .page-link{
    background: none;
  }

 ngb-pagination /deep/ .page-item.active .page-link {
    background-color: #223C61;
    &:focus, &:visited{
      outline: none;
      box-shadow: none;
    }
  }
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
s4tr2
  • 415
  • 1
  • 9
  • 27

2 Answers2

10

An alternative to using /deep/ or ::ng-deep is to disable View Encapsulation on your components that use third party UI elements.

@Component({
  selector: 'app-mycomp',
  templateUrl: './mycomp.component.html',
  styleUrls: ['./mycomp.component.scss'],
  encapsulation: ViewEncapsulation.None
})

Doing so means that:

Angular adds the CSS to the global styles. The scoping rules, isolations, and protections discussed earlier don't apply. This is essentially the same as pasting the component's styles into the HTML.

If you turn off view encapsulation make sure to target only the html element you really want, and not any other elements of the same type in different components of your app (e.g. add a custom class or id to your element).

When I disabled ViewEncapsulation I also had to use !important in my CSS to overwrite existing third party styles, while it wasn't always needed with ::ng-deep.

frido
  • 13,065
  • 5
  • 42
  • 56
  • It is the best option together with BEM – gsziszi Apr 15 '19 at 14:27
  • Remember that `ViewEncapsulation.None` bubbles up to the whole application, so if you are going with this solution is may be a good idea to wrap your CSS with a wrapper class. See also this answer https://stackoverflow.com/a/49308475/2275011 and comments for more details and solutions. – Ferie May 21 '19 at 10:16
3

::ng-deep, /deep/ and >>> deprecation The ::ng-deep pseudo-class selector also has a couple of aliases: >>> and /deep/, and all three are soon to be removed.

The main reason for that is that this mechanism for piercing the style isolation sandbox around a component can potentially encourage bad styling practices.

The situation is still evolving, but right now, ::ng-deep can be used if needed for certain use cases.

https://blog.angular-university.io/angular-host-context/

So use ::ng-deep instead

René Winkler
  • 6,508
  • 7
  • 42
  • 69