简体   繁体   English

使用 flex 垂直对齐项目会拉伸子元素

[英]Aligning items vertically with flex stretches child elements

I got multiple child elements ( span ) in a flex container and want to align them vertically using align-items: center :我在 flex 容器中有多个子元素 ( span ),并希望使用align-items: center垂直对齐它们:

<div class="w-full mb-1 flex items-center">
    <span class="border borderHighlight px-1" style="font-size: 0.6rem;">{{ badgeTitle }}</span>
    <span class="colorGray01 subText ml-1">&#183; {{ commentTimeStamp }}</span>
    <span class="negative subText font-extrabold text-sm ml-2">&bull;</span>
    <span>&#10003;</span>
</div>

Without the flexbox classes the border of the first span element is fitting the element itself.如果没有 flexbox 类,第一个 span 元素的边框将适合元素本身。 using flexbox the border is stretching to the biggest height of one of the child elements.使用 flexbox 边框拉伸到其中一个子元素的最大高度。

有弹性 没有弯曲

How would I keep the original size of the child element and aligning it vertically centered?我将如何保持子元素的原始大小并将其垂直居中对齐?

For those not used to Tailwind the style on the parent element literally is:对于那些不习惯 Tailwind 的人,父元素上的样式字面意思是:

display: flex;
align-items: center;
width: 100%;
margin-bottom: 1rem;

There are not styles on the child elements that affect this at all.根本没有影响此的子元素上的 styles。 Just some color/font stylings and margins/paddings.只是一些颜色/字体样式和边距/填充。 It's more of a general css question tho.这更像是一个一般性的 css 问题。

Fixed it by wrapping the <span> -Element inside another <span> .通过将<span> -Element 包装在另一个<span>中来修复它。 Not sure, why it solved the problem or if it's a good way to solve it.不确定,为什么它解决了问题,或者它是否是解决问题的好方法。 But it did the trick.但它成功了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM