Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off view encapsulation for one property in Angular 2?

I have the following styles:

#right_content {
  padding: 30px 40px !important;
}

I store this inside a file register.css, which is bound to my register.ts.

The problem is that <div id="right_content"> is located in a parent module, which means I can't directly modify its CSS properties from within register.ts.

<div id="right_content">
  <router-outlet></router-outlet>
</div>

My register.html and register.css goes into the router outlet. I want to style the #right_content from register.css.

Is there any way I can turn off view encapsulation (or whatever the adding of the _ngcontent-mxo-3 attributes is called), just for the above styles?

like image 873
Lucas Avatar asked May 05 '16 05:05

Lucas


People also ask

What is the default view encapsulation mode in Angular?

Emulated is the recommended and default mode. Angular modifies the component's CSS selectors so that they are only applied to the component's view and do not affect other elements in the application (emulating Shadow DOM behavior).

How many encapsulation techniques are there in Angular?

Angular provides three encapsulation strategies: Emulated (default) - styles from main HTML propagate to the component. Styles defined in this component's @Component decorator are scoped to this component only.

What is Shadow DOM in Angular?

Shadow DOM is like a parallel DOM tree hosted inside a component (an HTML element, not to be confused with Angular components), hidden away from the main DOM tree. No part of the application has access to this shadow DOM other than the component itself.


1 Answers

update

::slotted is now supported by all new browsers and can be used with `ViewEncapsulation.ShadowDom

https://developer.mozilla.org/en-US/docs/Web/CSS/::slotted

original

It is supported to create selectors that go through component boundaries even when ViewEncapsulation is Emulated (default)

child-component ::ng-deep #right_content {
  padding: 30px 40px !important;
}

Allows to tile the <xxx id="right_content"> from any ancestor

like image 72
Günter Zöchbauer Avatar answered Nov 15 '22 23:11

Günter Zöchbauer