Light DOM style cascades down to the shadow DOM

不羁的心 提交于 2019-12-02 12:18:14

问题


I have created a custom element with polymer. When the element is included within an h2, it inherits the h2's boldness and font-size. I need my components to be sheltered from the outside world and not be affected by light dom styles. How can I achieve this if the light DOM cascades down?

To be more specific, check out the following:


回答1:


This appears to be by design:

The top-level elements of a shadow tree inherit from their host element.

The host element in this case is the h2.

You will need to include explicit size and weight declarations in your custom element's CSS in order to prevent it from inheriting the styles from its host element.




回答2:


You can reset your CSS to the browsers defaults using all: initial; in the :host

:host {
    all: initial;
}

Not supported in IE or Edge but neither is shadow DOM.

Another option is to use a CSS reset in your web component such as normalize.css

Apparently there are browser optimisations in place to handle identical CSS in multiple web components so it's not as inefficient as it sounds.



来源:https://stackoverflow.com/questions/30147942/light-dom-style-cascades-down-to-the-shadow-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!