【发布时间】:2022-11-25 19:03:18
【问题描述】:
如果您在影子根子节点上切换 display:flex,它也会影响外部元素。 (所有大型浏览器的行为都是如此。)为什么?
有一个带有影子根的 Web 组件:
<web-comp style="display: inline-block;"></web-comp>
在 shadow root 里面有一个带有 display:flex 的 div:
div.style="display:flex; align-items:center; height:50px;"
完整的例子:
class demo extends HTMLElement {
connectedCallback() {
const shadow = this.attachShadow({mode: 'open'});
const div = document.createElement('div');
div.innerHTML= "I am in a shadow root!"
div.style="display:flex;align-items:center;height:50px;background:lightblue"
shadow.appendChild(div);
}
}
customElements.define('web-comp', demo);
<h3>flexbox styles do not respect shadow root border</h3>
<web-comp style="display: inline-block;"></web-comp>
And I am not.
<button onclick="document.querySelector('web-comp').shadowRoot.querySelector('div').style.alignItems='baseline'">
Click to change 'align-items' of div in shadow root.
</button>
【问题讨论】:
-
你能把你的 JSFiddle 转换成a StackOverflow snippet吗?你的问题将保留在 SO 上,但你的 JSFiddle 可能会在几年后消失
标签: javascript html css custom-element shadow-root