Home > Article > Web Front-end > How can I style child components from the parent in Angular using ::ng-deep?
Styling Child Components from Parent: Utilizing ::ng-deep in Angular
Introduction
In Angular 4, developers can face situations where it becomes necessary to alter CSS properties of child components from the parent component. This requires a method to penetrate through the shadow DOM boundaries and target the desired styles. The ::ng-deep selector provides a solution for this challenge.
Usage of ::ng-deep
::ng-deep is a shadow-piercing selector that allows access to the styles of child components from the parent component. It is the current recommended alternative to the deprecated /deep/ combinator, and it shares the same alias with >>>. By utilizing ::ng-deep, styles applied in the parent component can cascade down and affect the child components.
Example
To illustrate the usage of ::ng-deep, consider the following code:
<code class="html"><div class="overview tab-pane" id="overview" role="tabpanel" [innerHTML]="project?.getContent('DETAILS')"></div></code>
<code class="css">.overview { ::ng-deep { p { &:last-child { margin-bottom: 0; } } } }</code>
In this example, the styles applied to the .overview class using ::ng-deep will be applied to the p elements within the child component of the .overview class. The &:last-child pseudo-class will affect the last p element in that child component, removing the default bottom margin.
IE11 Support
It's important to note that ::ng-deep is not supported in IE11. For browsers that do not support ::ng-deep, alternative methods such as ViewEncapsulation.None and encapsulation: "none" in Angular CLI projects can be explored to achieve similar functionality.
The above is the detailed content of How can I style child components from the parent in Angular using ::ng-deep?. For more information, please follow other related articles on the PHP Chinese website!