如何使用flex垂直对齐一个Stencil组件与一个HTML元素?

huangapple go评论46阅读模式
英文:

How to vertically align a Stencil component with an HTML element using flex?

问题

返回内容如下:

return (
    <a href="javascript:;" class={`dropdown-item ${this.checkboxColor}`}>
        {this.checkable && <input type="checkbox" id="checkbox4" class={`form-check-input`} />}
        <ifx-icon icon={this.icon}></ifx-icon>
        <label htmlFor="checkbox4" class="form-check-label"><slot /></label>
    </a>
)

建议您使用以下方式垂直对齐ifx-icon<label>,在<a>元素的样式中添加display: flex; align-items: center;,以确保ifx-icon<label>都受到垂直居中对齐的影响,就像下面这样:

return (
    <a href="javascript:;" class={`dropdown-item ${this.checkboxColor}`} style="display: flex; align-items: center;">
        {this.checkable && <input type="checkbox" id="checkbox4" class={`form-check-input`} />}
        <ifx-icon icon={this.icon}></ifx-icon>
        <label htmlFor="checkbox4" class="form-check-label"><slot /></label>
    </a>
)
英文:
return (
       &lt;a href=&quot;javascript:;&quot; class={`dropdown-item ${this.checkboxColor}`}&gt;
          {this.checkable &amp;&amp; &lt;input type=&quot;checkbox&quot; id=&quot;checkbox4&quot; class={`form-check-input`} /&gt;}
          &lt;ifx-icon icon={this.icon}&gt;&lt;/ifx-icon&gt;
          &lt;label htmlFor=&quot;checkbox4&quot; class=&quot;form-check-label&quot;&gt;&lt;slot /&gt;&lt;/label&gt;
        &lt;/a&gt;
    )

I want to vertically-align ifx-icon with the &lt;label&gt; using flex and align-items: center.
The container dropdown-item already has flex applied, but it's not being applied to the ifx-icon&#39;s element, because as you know, styles are applied to the component wrapper itself, not the element that's inside.

What do you suggest I do in this case?

答案1

得分: 0

正如您所述:

样式应用于组件包装器本身,而不是内部的元素

这意味着您需要确保位于主机元素内部的元素或元素已正确布局并响应所需应用于主机元素的任何样式。

假设您分享的render函数是为dropdown-item组件,那么将样式应用于该组件只会影响<a>元素,而不会影响其内容,正如您所了解的。您需要向dropdown-item组件添加样式,以便<a>元素布局图标和标签属性。

这可能看起来像这样:

dropdown-item.scss

:host {
    a.dropdown-item {
        display: inline-flex;
        align-items: center;
    }
}

您还可以使用CSS部分从组件外部应用样式:

return (
    <a part="container" ...>
        {this.checkable && <input type="checkbox" id="checkbox4" class={`form-check-input`} />}
        <ifx-icon icon={this.icon}></ifx-icon>
        <label htmlFor="checkbox4" class="form-check-label"><slot /></label>
    </a>
)
dropdown-item::part(container) {
    display: inline-flex;
    align-items: center;
}
英文:

As you stated:

> styles are applied to the component wrapper itself, not the element that's inside

which means that you need to make sure the element or elements that are inside the host element are properly laid out and respond to any style applied to the host element that is desired.

Assuming the render function you shared is for the dropdown-item component, applying style to that component would only affect the &lt;a&gt; element, not its contents, as you already know. You need to add style to your dropdown-item component so that the &lt;a&gt; lays out the icon and label property.

That would probably look something like:

dropdown-item.scss

:host {
    a.dropdown-item {
        display: inline-flex;
        align-items: center;
    }
}

You might also be able to use CSS parts to apply the style from outside the component:

return (
    &lt;a part=&quot;container&quot; ...&gt;
        {this.checkable &amp;&amp; &lt;input type=&quot;checkbox&quot; id=&quot;checkbox4&quot; class={`form-check-input`} /&gt;}
        &lt;ifx-icon icon={this.icon}&gt;&lt;/ifx-icon&gt;
        &lt;label htmlFor=&quot;checkbox4&quot; class=&quot;form-check-label&quot;&gt;&lt;slot /&gt;&lt;/label&gt;
    &lt;/a&gt;
)
dropdown-item::part(container) {
    display: inline-flex;
    align-items: center;
}

huangapple
  • 本文由 发表于 2023年2月16日 15:33:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定