Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 !!!
}
like image 809
J.BizMai Avatar asked Feb 23 '17 12:02

J.BizMai


People also ask

Can I style ng-content?

ng-content is an important concept to create reusable and flexible components. With the help of ng-content content can be projected by a parent component into a predefined slot. In some cases, you want to apply some styles to the projected content.

Can I use CSS in Angular?

Angular applications are styled with standard CSS. That means you can apply everything you know about CSS stylesheets, selectors, rules, and media queries directly to Angular applications. Additionally, Angular can bundle component styles with components, enabling a more modular design than regular stylesheets.


1 Answers

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.

like image 188
Vilmantas Baranauskas Avatar answered Sep 28 '22 01:09

Vilmantas Baranauskas