What i have understood of host is that if i have a child component inside a parent component and we want to style a child component from the parent component we can use :host . and :host-context for vice-versa. Please let me know if this is the right use of host .
https://angular.io/docs/ts/latest/guide/component-styles.html
When i try to do the same in my App it dosent work
App component template
<div class ="top">
<h1>
Home Component
</h1>
<hr>
<app-ngrx></app-ngrx>
<router-outlet></router-outlet>
<div>
ngrx component template
<h3 class="mine">NGRX</h3>
<button (click)="increment()">Increment</button>
<div>Current Count: {{ counter | async }}</div>
<button (click)="decrement()">Decrement</button>
<button (click)="reset()">Reset Counter</button>
App component CSS
:host(.mine){
color:red;
}
This do not Seem to work Please help i am not able to understand.
I looked at this question But just not able to figure out
Angular 2: How to style host element of the component?
Updated after @Gunter Answer
In my app-ngrx template i have added
<h3 class = "mine">NGRX</h3>
<button (click)="increment()">Increment</button>
<div>Current Count: {{ counter | async }}</div>
<button (click)="decrement()">Decrement</button>
<button (click)="reset()">Reset Counter</button>
and in the app-ngrx css file i have added
:host(.mine){
color:red;
}
But even without adding mine in app component like
<app-ngrx></app-ngrx>
The h3 is red where as i feel it should be red when <app-ngrx class = "mine"></app-ngrx>
No, this is not what it used for.
:host
selector comes from shadow DOM spec.In angular world, a component's template is a shadow tree. The component's element is a shadow host. So when you're defining styles for
:host
selector, the styles are applied to the component's element.:host
In your example, if you defined styles in
my-app
component, the styles will be applied to<my-app>
DOM element. This particular configuration:Will be applied to the host element that has
.mine
class:If you defined styles in
app-ngrx
component, the styles will be applied to<app-ngrx>
DOM element, NOT<my-app>
. This particular configuration:Will be applied to the host element that has
.mine
class::host-context
Now,
:host-context
is also applied to the host element, but the function (parenthesis) takes a selector that is checked not against the host element itself, but against all ancestors up to document root. If such element is found, the styles are applied.For example, this selector
matches such structure:
whereas, this selector:
matches this structure:
This is useful, if you want to apply styles to components view (shadow root) conditionally. This makes
h2
always bold:whereas this
makes them bold only if your component is inside an element with class
.make-inner-components-bold
.:host { ... }
selects the component itself:host(.mine) { ... }
selects the component itself when it hasclass="mine"
set:host-context(.mine) { ... }
selects the component itself when one of its ancestors hasclass="mine"
setSee also https://angular.io/docs/ts/latest/guide/component-styles.html
or with
:host-context
AppComponent
or with
class="mine"
setAppComponent
update
If you want to style the content of a child component (instead of the child component itself) you can use
/deep/
update 2
::slotted
is now supported by all new browsers and can be used with `ViewEncapsulation.ShadowDomhttps://developer.mozilla.org/en-US/docs/Web/CSS/::slotted