最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to select :host with a class in css for web components? - Stack Overflow

programmeradmin4浏览0评论

In the new v1 web ponents 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

In the new v1 web ponents 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

Share Improve this question edited Aug 18, 2017 at 4:20 omega asked Aug 18, 2017 at 4:11 omegaomega 43.9k89 gold badges285 silver badges521 bronze badges 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? – Jason Goemaat Commented Aug 18, 2017 at 4:24
  • I tried doing .a:host and :host('.a') but neither worked. – omega Commented Aug 18, 2017 at 4:29
  • Are you using chrome? If not this question will explain why its not working stackoverflow./questions/25468701/… – Carl Pease Commented Aug 18, 2017 at 4:29
  • I am using latest version of chrome actually. – omega Commented Aug 18, 2017 at 4:29
  • this worked :host(.a) – omega Commented Aug 18, 2017 at 4:48
Add a ment  | 

2 Answers 2

Reset to default 11

As per the documentation:

For example, say you had a ponent 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 mented, that would, in this case, translate to:

:host(.a) {
    ...
}

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

发布评论

评论列表(0)

  1. 暂无评论