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






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






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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ