Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why CSS direct descendant selector is not applied on Angular component

Tags:

html

css

angular

We have this simple main.html

<app>
   <sidebar></sidebar>
   <main-content>
      <router-outlet></router-outlet>
   </main-content>
</app>

When we load a simple component via routing (URL change), the HTML becomes:

<app>
   <sidebar></sidebar>
   <main-content>
      <router-outlet></router-outlet>
      <component></component>
   </main-content>
</app>

The first question is why <component> is not nesting inside <router-outlet>, and becomes a sibling.

However, this CSS is not applied on <component> tag:

app sidebar main-content > * {
   background: red;
}

When we check, <router-outlet> has this style, but <component> which is the direct child of <main-content> doesn't have this style.

What is wrong here?

like image 536
Sajad Sedghizadeh Avatar asked Dec 18 '25 22:12

Sajad Sedghizadeh


1 Answers

Based on your stated code:

<app>
   <sidebar></sidebar>
   <main-content>
      <router-outlet></router-outlet>
      <component></component>
   </main-content>
</app>

this

app sidebar main-content > * {
   background: red;
}

will not work because main-content is not a child of sidebar but is a sibling.

I'd suggest you try

app sidebar + main-content > * {
   background: red;
}

or even

app main-content > * {
   background: red;
}
like image 157
Paulie_D Avatar answered Dec 21 '25 14:12

Paulie_D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!