首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS :部件伪选择器可以用来样式嵌套的web组件吗?

CSS :部件伪选择器可以用来样式嵌套的web组件吗?
EN

Stack Overflow用户
提问于 2022-02-09 10:14:09
回答 2查看 1.3K关注 0票数 5

当使用shadowed创建具有封装样式的Web组件时,阴影标记的部分可以使用::part伪选择器(https://developer.mozilla.org/en-US/docs/Web/CSS/::part)进行样式设计。

零件选择器可以用来瞄准嵌套的阴影部件吗?

代码语言:javascript
复制
<custom-element>
  #shadow-root
    <div part="thats-easy"></div>
    <another-custom-element part="proxy-part">
      #shadow-root
        <div part="target-me"></div>
    </another-custom-element>
</custom-element>

目前的努力没有结果:

代码语言:javascript
复制
another-custom-element::part(target-me) { }
custom-element::part(proxy-part) another-custom-element::part(target-me) { }
custom-element::part(proxy-part another-custom-element::part(target-me)) { }
custom-element::part(proxy-part::part(target-me)) { }
代码语言:javascript
复制
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-09 15:51:21

不是的。这是不可能的。它破坏了封装原理。正确的方法是使用适当的主题。这意味着使用以下两种方法的组合:

代码语言:javascript
复制
::part - For direct theming of the component
:host-context - for theming based on the context
::slotted - For styling the slotted element inside the styling

对于更多的动态主题化,您可以将上述样式与Element.matches API结合使用。无论组件的用户设置了什么类/上下文,都可以更改嵌套子组件的样式。

附带说明,即使不使用Shadow或Web,修改颓废组件的样式也是一种糟糕的做法。它将导致一个脆弱的、不可维护的CSS。

编辑注意事项:

:host-context并不是在所有浏览器中都实现的,而且可能永远也不会实现。

票数 3
EN

Stack Overflow用户

发布于 2022-07-19 18:45:27

有趣的转折,这是可能的,关于exportparts https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/exportparts

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71047699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档