I've got a parent component:
<parent></parent>
And I want to populate this group with child components:
<parent>
<child></child>
<child></child>
<child></child>
</parent>
Parent template:
<div class="parent">
<!-- Children goes here -->
<ng-content></ng-content>
</div>
Child template:
<div class="child">Test</div>
Since parent
and child
are two separate components, their styles are locked to their own scope.
In my parent component I tried doing:
.parent .child {
// Styles for child
}
But the .child
styles are not getting applied to the child
components.
I tried using styleUrls
to include the parent
's stylesheet into child
component to solve the scope issue:
// child.component.ts
styleUrls: [
'./parent.component.css',
'./child.component.css',
]
But that didn't help, also tried the other way by fetching the child
stylesheet into parent
but that didn't help either.
So how do you style child components that are included into a parent component?
Best Answer
Update - Newest Way
Don't do it, if you can avoid it. As Devon Sans points out in the comments: This feature will most likely be deprecated.
Last Update
From Angular 4.3.0 till even now (Angular 12.x), all piercing css combinators were deprecated. Angular team introduced a new combinator
::ng-deep
as shown below,DEMO : https://plnkr.co/edit/RBJIszu14o4svHLQt563?p=preview
Old way
You can use
encapsulation mode
and/orpiercing CSS combinators >>>, /deep/ and ::shadow
working example : http://plnkr.co/edit/1RBDGQ?p=preview