12

In the new v1 web components specs, you can use :host to target the custom element. But then I attached a class A to the custom element, and had a css selector :host.A, but its styles are not being applied. Does anyone know how to fix this?

Thanks

5
  • Can you try .a:host and :host('.a')? Can you give the exact css and an example of what the element looks like? From my reading it seems like it is a pseudo-class and pseudo-classes can appear anywhere in a sequence of simple selectors. Also what browser are you using, and does it specify it supports it? Commented Aug 18, 2017 at 4:24
  • I tried doing .a:host and :host('.a') but neither worked. Commented Aug 18, 2017 at 4:29
  • Are you using chrome? If not this question will explain why its not working stackoverflow.com/questions/25468701/… Commented Aug 18, 2017 at 4:29
  • I am using latest version of chrome actually. Commented Aug 18, 2017 at 4:29
  • this worked :host(.a) Commented Aug 18, 2017 at 4:48

2 Answers 2

11

As per the documentation:

For example, say you had a component with a shadow tree like the following:

<x-foo class="foo">
    <"shadow tree">
        <div class="foo">...</div>
    </>
</x-foo>

For a stylesheet within the shadow tree:

  • ...
  • ':host(.foo)' matches the <x-foo> element.

As OP has commented, that would, in this case, translate to:

:host(.a) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe this shouldn't work, but for me it does😬:

:host-context(.dialog) .overlay { background: green; }

for a class, directly attached to the template (not something higher up)

<my-component class='dialog><div class='overlay'></div></my-component>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.