Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right way to override child component style from host component angular

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

like image 699
Hacker Avatar asked Jan 23 '19 13:01

Hacker


People also ask

How do you override component styles?

If you want to override a component's styles using custom classes, you can use the className prop, available on each component.

Is it possible for a child component to inherit styles CSS from a parent 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.

What selectors would force this style from the host and down through the child component tree into all child component views host h3 background color yellow?

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.

How do I apply CSS to child element based on parent?

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.

How to add child Styles to the host element in angular?

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.

How to override Angular Material’s styles?

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.

What is child component from parent in angular CSS?

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.

Is it possible to spread CSS styles outside of an angular component?

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.


2 Answers

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.

like image 55
ConnorsFan Avatar answered Oct 10 '22 02:10

ConnorsFan


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>
like image 26
Eliseo Avatar answered Oct 10 '22 02:10

Eliseo