I've been looking for a definitive answer to this question for a long time. Is there a reliable and recommended alternative strategy to do this yet? Incorrect answers to this question include:
Just favor ::ng-deep for now
and
if component author didn't integrate styling into their API, you're out of luck
According to the docs, all 3 of these combinators are deprecated, so what's the "right way" of going about this?
The answers suggesting using a strategy of global styling literally answer the question and are appreciated. However, Angular is a component-based framwork, and view encapsulation is one of the core boons that make it a valuable tool. To be fair to those providing an answer, this was not specified in the question. Still, the general use-case and desired behavior is to keep view encapsulation, so such a dramatic change in workflow is not a reasonable solution for the majority of cases, i.e. a "right way".
::ng-deep is what's called a shadow-piercing descendant combinator. It works because it is able to pierce the shadow DOM. Using ::ng-deep to override a child components styles will achieve the desired result but with unintended side effects.
Angular's API states that the ng-deep psuedo-class is deprecated. completely disables view-encapsulation for that rule. If we use it without the :host pseudo-class, it will make the style-rule global, not a good thing. There's something odd about Angular view encapsulation which gets style specificity wrong.
There are several ways to add styles to a component: By setting styles or styleUrls metadata. Inline in the template HTML. With CSS imports.
You cannot place the style globally unless you want to change all the instances. Currently, there is no correct way to achieve what you ask.
The angular documentation states that /deep/ and >>> are deprecated. ng-deep, is deprecated too, but the documentation does not provide a way to achieve the desired goal to fix a 3rd party component instance in angular. The common practice inferred by the documentation is to use the deprecated ng-deep operator while the angular team figures out what to do.
Clearly, the user of ng-deep is not clean, but there is no alternative at the moment.
Add them to your global styles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With