Modifying the CSS of a template of a component in another component
Modifying the CSS of a template of a component in another component
I am calling component A as a child-component in component B. Component A has a div class containing CSS that I need to modify in Component B. How do I access that div class from component B?
div
div
Template of component A:
<div class="something">
.
.
</div>
Template of component B:
<app-componentA></app-componentA>
1 Answer
1
You shouldn't really do that, you can have an input of the styles/classes you want applied for the child component, however if you need to do it, you can use ngdeep (deprecated) here
EDIT
You can pass an input (which indeed is just a JavaScript object) from the parent component, and assign that dynamically in the child component like that:
Child component:
<div [ngStyle]="inputStyle">
</div>
And its TS:
@Input() inputStyle; //This is the input received from the parent component
Then in the parent component, you can pass the CSS as a JavaScript object normally like the following :
<app-componentA [inputStyle]="backgroundColor: 'red', 'height': '100px'"></app-componentA>
This example sets the style to a static styles object passed through the HTML, of course this can be used with dynamic object (that you assign through the parent's TS file), or you can even pass custom class names using the same concept but using ngClass instead of ngStyle in the child component
ngClass
ngStyle
like: <app-componentA [variable]="value"></app-componentA>
– Avinash Prabhakar
Sep 12 '18 at 18:09
How do I do it for CSS?
– Avinash Prabhakar
Sep 12 '18 at 18:09
This should be a comment not an answer
– Patricio Vargas
Sep 12 '18 at 18:11
@xploshioOn Sorry, I was away, it is indeed an answer, ::ng-deep does exactly what he wants to achieve, I just edited my answer to show the alternative (should have done so from the start tbh :)
– abdullahkady
Sep 12 '18 at 21:38
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Could you elaborate a bit on the first part? I would like to know how to have an input of the styles. From what I googled, input output only seems to apply to javascript variables.
– Avinash Prabhakar
Sep 12 '18 at 18:08