1

I am having one table in my component with certain width .The problem is when I am reusing that component selector in another component I am not able to override table style in angular.

I tried below CSS:

:host /deep/ component-selector {
  width:50px;
}
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84

3 Answers3

1

That is because of view encapsulation. You can turn it of or write css styles that break with

::ng-deep your style

Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
  • I have used :host /deep/ component-selector{width:50px} but still not able to override selector – Sonali Pawde Jun 21 '19 at 05:34
  • 1
    @SonaliPawde also try setting `encapsulation: ViewEncapsulation.None,` in the reusable component ts code and also make sure you set `!important` flag to css – Joel Joseph Jun 21 '19 at 05:40
0

Try with following

/deep/ <css selector>{width:50px}

If still doesn't work the use !important

/deep/ <css selector>{width:50px !important}
Sunil Singh
  • 11,001
  • 2
  • 27
  • 48
0

According to angular style guide , the style should not flow from parent to child ie -> the component that has your < component-selector > should not take care of child's style.

What you should do is, make use of host-context.

Apply a class to the parent (let's say widht-mid). In the child's css you need to check that does my parent has a class widht-mid, if yes then it should change it's width.

In the child's css file ->

:host-context(.width-mid) table{
width: 50px
}

You should not transfer styles from parent to child essentialy because it spoils the ViewEncapsulation goodness that Angular is proud of. Here is some refrence . :Style Guide (also the above answers are deprecated, and angular team is thinking of removing them entirely)

Also, the child component should be responsible for how does it table looks. The parent should be concerned about itself. In the future, if you will have two children to your parent (it two < component-selector > . in one parent), it will be difficult to manage what style to pass to what child. Using this method, altering style is not only easy but also manageable.

Upvote and mark as solved if I was able to help.Cheers.

ishan srivastava
  • 363
  • 1
  • 3
  • 10