Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop global styles from affecting my component

Tags:

angular

Is there a way to seal my Angular component off from the global style sheet? I don't want the styles in the global style sheet to take effect inside my component.

I have tried this

.reset {
  all: initial;
  * {
    all: unset;
  }
}

but is there not a different way?

like image 211
Jayme Avatar asked May 23 '19 15:05

Jayme


People also ask

How do I stop global CSS override?

You can use the :not selector in your global CSS.

How do I prevent angular component styling override from carrying over to other components?

You need to Emulate the encapsulation property on your Component .

Can a component be styled with a global style sheet?

The important thing to remember is to place the GlobalStyle component as a sibling component to your main application component(s). And that's it! Global styling is now all set up with Styled Components.


2 Answers

In your component decorator, add:

@Component({
    selector: '..',
    templateUrl: '..',
    styleUrls: ['..'],
    encapsulation: ViewEncapsulation.ShadowDom 
})

This will create a shadow dom and will force your component to use styles only from the provided styles/styleUrls array.

If you are using angular version < v6.1.0. You can use ViewEncapsulation.Native. It's deprecated after that.

like image 137
Akshay Rana Avatar answered Oct 03 '22 06:10

Akshay Rana


try this, it must work:

:host ::ng-deep .reset{

  //your style
}
like image 44
Doflamingo19 Avatar answered Oct 03 '22 06:10

Doflamingo19