Is there a way to use css in ng-content inside an Angular2 component?

J.BizMai picture J.BizMai · Feb 23, 2017 · Viewed 17.6k times · Source

I tried to include css for children element included in a component via ng-content. It seems to be not implemented yet in Angular 2 or maybe someone has got a solution except to put css in a general stylesheet ?

app.component.ts

<comp-parent>
  <comp-child></comp-child>
</comp-parent>

compParent.component.html

<div class="wrapper">
  <ng-content></ng-content>
</div>

compParent.component.css

.wrapper > comp-child {
   margin-right: 5px; <-- Not applied !!!
}

Answer

Vilmantas Baranauskas picture Vilmantas Baranauskas · Feb 23, 2017

You can use /deep/ (deprecated) or >>> or ::ng-deep selector:

https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep

E.g.:

.wrapper ::ng-deep comp-child { ... }

Note that >>> seems to not work for projects based on angular-cli.