Web 组件可以有 Flexbox 子组件吗?

发布于 2025-01-11 20:24:10 字数 487 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七秒鱼° 2025-01-18 20:24:10

好吧,让我们使用这两种变体来检查一下:

customElements.define('custom-element-with-shadowroot', class extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({mode:'open'}).innerHTML = `
      <style>
        :host { display: flex; flex-direction: row; gap: 20px; height: 50px; width: 200px; }
        div { background-color: orange; }
      </style>
      <div>1</div>
      <div>2</div>
      <div>3</div>      
    `;
  }
});

customElements.define('custom-element-without-shadowroot', class extends HTMLElement {
  constructor() {
    super();
    this.innerHTML = `
      <style>
        custom-element-without-shadowroot { display: flex; flex-direction: row; gap: 20px; height: 50px; width: 200px; }
        div { background-color: orange; }
      </style>
      <div>1</div>
      <div>2</div>
      <div>3</div>      
    `;
  }
});
<custom-element-with-shadowroot></custom-element-with-shadowroot>
<hr>
<custom-element-without-shadowroot></custom-element-without-shadowroot>

正如您所看到的,无论有没有 Shadow DOM,自定义元素本身都可以充当 Flex 容器。请注意 :host 仅适用于影子 DOM。

Well, let's check using both variants:

customElements.define('custom-element-with-shadowroot', class extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({mode:'open'}).innerHTML = `
      <style>
        :host { display: flex; flex-direction: row; gap: 20px; height: 50px; width: 200px; }
        div { background-color: orange; }
      </style>
      <div>1</div>
      <div>2</div>
      <div>3</div>      
    `;
  }
});

customElements.define('custom-element-without-shadowroot', class extends HTMLElement {
  constructor() {
    super();
    this.innerHTML = `
      <style>
        custom-element-without-shadowroot { display: flex; flex-direction: row; gap: 20px; height: 50px; width: 200px; }
        div { background-color: orange; }
      </style>
      <div>1</div>
      <div>2</div>
      <div>3</div>      
    `;
  }
});
<custom-element-with-shadowroot></custom-element-with-shadowroot>
<hr>
<custom-element-without-shadowroot></custom-element-without-shadowroot>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文