英文:
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 (
<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>
)
I want to vertically-align ifx-icon
with the <label>
using flex
and align-items: center
.
The container dropdown-item
already has flex applied, but it's not being applied to the ifx-icon'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 <a>
element, not its contents, as you already know. You need to add style to your dropdown-item
component so that the <a>
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 (
<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论