Web 组件可以有 Flexbox 子组件吗?
CSS Flexbox 布局充当元素之间的父子关系。父元素设置为 display: flex
并且其子元素成为 flex 子元素。
我正在寻找一个明确的答案,了解如果将 Web 组件(或自定义元素)设置为 Flex 父级会发生什么情况。自定义元素的 Flex 子元素是什么(如果有)?并且,答案是否会根据是否启用 Shadow DOM 而改变。
在父文档中:
<my-custom-element style="display: flex"></my-custom-element>
在自定义元素模板中:
<div style="flex: auto">A</div>
<div style="flex: auto">B</div>
<div style="flex: auto">C</div>
CSS Flexbox layout works as a parent-child relationship between elements. A parent element is set to display: flex
and its children become flex children.
I'm looking for a clear answer on what's supposed to happen if a web component (or custom element) is set as a flex parent. What are the flex children of a custom element, if any? And, does the answer change based on whether shadow DOM is enabled or not.
In parent document:
<my-custom-element style="display: flex"></my-custom-element>
In custom element template:
<div style="flex: auto">A</div>
<div style="flex: auto">B</div>
<div style="flex: auto">C</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,让我们使用这两种变体来检查一下:
正如您所看到的,无论有没有 Shadow DOM,自定义元素本身都可以充当 Flex 容器。请注意
:host
仅适用于影子 DOM。Well, let's check using both variants:
As you can see, both with and without shadow DOM the custom element itself can act as a flex container. Please note that
:host
is only available with shadow DOM.