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

Turning the background color red using the :host selector?

In this stackblitz demo the background turns red with this selector:

  styles: [
    `
    :host .background-div {
      background-color: red;
    }
  `,
  ]

However if we remove the .background-div part of the selector and only use the :host selector like this:

    :host {
      background-color: red;
    }

The background of the element is not turned red. Just curious why?

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

>Solution :

According to documentation on MDN:

The :host CSS pseudo-class selects the shadow host of the shadow DOM
containing the CSS it is used inside — in other words, this allows you
to select a custom element from inside its shadow DOM.

So something like

  :host {
      background-color: red;
    }

Will have no effect because it is used outside a shadow DOM.

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