What is the right way to override child component style from host component. I tried using encapsulation: ViewEncapsulation.None
but i need to write the override stuff in style.sass file rather than host component. What should be the stackblitz
If you want to override a component's styles using custom classes, you can use the className prop, available on each component.
A parent component can style a child component, but it styles it as a single element. A parent can't reach into a child.
Using /deep/ selector we can force a style down through the child component tree into all child component views. /deep/ selector forces its style to its own component, child component, nested component, view children and content children.
It's easy to apply style to a child element, but if you want to apply style to a parent class that already has child elements, you can use the CSS selector child combinators (>), which are placed between two CSS selectors.
Technically, it could be done as follows: In other words, you use :host pseudo-selector provided by Angular + set of CSS classes to define possible child styles in child component itself. You then have the ability to trigger those styles from outside by applying pre-defined classes to the <child> host element. Actually there is one more option.
Angular Material uses the least specific selectors possible for its components to make it easy to override them. More specific styles will take precedence over less specific styles. So in our case, we have to add more specificity to overwrite our AM styles. The best way to do so is to use the name of our component’s selector.
Styling child component from parent in Angular March, 1, 2021 angular css Angular provides a modular design that encourages the developer to create separate components with its own logic and styles. This approach has many advantages, but it can cause some problems to solve.
It is possible to spread css styles outside of the Angular component. However, it may cause some unexpected styling issues so try to reduce the usage of this approach. If there's a need to apply styles for the descendant elements, use the ::ng-deep pseudo-class.
If you have control on the child component code, you can define a customStyle
input property:
@Input() customStyle: {};
<div class="child-div" [ngStyle]="customStyle">I am the child</div>
and pass it from the parent component:
<app-child [customStyle]="style1"></app-child>
style1 = {
backgroundColor: "red",
height: "150px"
}
See this stackblitz for a demo.
A similar technique can allow to pass a specific style attribute to the child component:
@Input() backgroundColor: string;
<div class="child-div" [style.background-color]="backgroundColor">I am the child</div>
from the parent component:
<app-child backgroundColor="red"></app-child>
See this stackblitz for a demo.
Otherwise, until an alternative method is proposed by Angular, you can use the ::ng-deep
shadow-piercing descendant combinator to modify the child component styling from the parent CSS:
:host ::ng-deep .parent .child-div {
background-color: lime;
height: 200px;
}
See this stackblitz for a demo.
My "way" is using viewEncapsulation.None, important and add class to the child. the forked stackblitz's Connors
//The parent
.child1 .child-div {
background-color: lime!important;
height: 200px!important;
}
<div style="text-align: center;" class='parent'>Hi I am the app-root and I contain child-comp!
<app-child class="child1"></app-child>
<app-child ></app-child>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With