Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set a background for a child component in Angular?

I have a parent component called landing.component, which has curretly two child componentes:

<app-land-top class="land-top"></app-land-top>
<app-land-btn></app-land-btn>

And I would like to set a SVG background to one of them.
I tried doing using the CSS from my parent component, landing.component.css, with:

.land-top {
  background: #141414 url(assets/entireTop_bg.jpg) no-repeat top left;
  background-size: cover;

But it did’nt work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I did find some questions here at StackOverflow, but I could’nt implement any of them.
They seemed a little confusing.

The ones I took a read were:

I believe that the following question is probably what I need, which is this one, that the user who answered talked about using NgClass, but I did’nt understand.

I tried adding [ngClass] = "land-top" to see if it worked, but it did’nt do nothing.

Can someone please help me?

>Solution :

The probable reason that it is not working is because in Anguler, parent component css does not apply/affect the child component.

In your case, follow below steps.

  1. Define the CSS in the app-land-top.
    component.css file
<app-land-top class="land-top" [ngClass]="{'land-top-bg': condition}"></app-land-top>
  1. and in your html
<app-land-top class="land-top"></app-land-top>

Let me know if it works.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading