I have a problem with vertically aligning text using flex
in my web application using Tailwind
. Here is part of my code:
我在我的Web应用程序中使用TailWind使用Flex垂直对齐文本时遇到了问题。以下是我的部分代码:
<div className="flex flex-col gap-4">
<div className="font-bebas bg-font-color ">
<h1 className="text-font-color text-4xl flex flex-row gap-2 items-end">nothing but
<p className="text-secondary text-5xl self-end">the best</p></h1>
</div>
<h2 className="text-secondary font-nautigal text-5xl">Frank Sinatra</h2>
</div>
My goal is to have the text "nothing but" and "the best" in the same line. How can I achieve this?
我的目标是把“只有”和“最好的”这两个字放在同一行。我怎样才能做到这一点呢?
更多回答
They are already in the same line. What am i missing?
他们已经在同一条线上了。我遗漏了什么?
@M.ÇağlarTUFAN You can notice that THE BEST is little bit higher, so it does look weird
@M.UnağlarTUFAN你可以注意到最好的稍微高一点,所以它看起来确实很奇怪
It's just due to selection highlight, but the texts are actually on the same base line. If you want selection to looks the same height, you can set both of them a same value of line-height
CSS property. This line-height
will make both texts to occupy same amount of height within their parent element.
这只是因为选择突出显示,但文本实际上在同一基线上。如果希望所选内容看起来高度相同,可以将它们设置为相同的行高css属性值。此行高将使两个文本在其父元素中占据相同的高度。
@M.ÇağlarTUFAN it is good an idea, setted line-heigt
as leading-3
for both texts, but THE BEST text looks little bit cutted of. See imgur.com/1i8c2nY
@M.?ğlarTUFAN这是一个很好的想法,将两个文本的行高设置为前导3,但最好的文本看起来有点被剪掉了。参见imgur.com/1i8c2nY
It's probably caused by the different font-sizes on texts. When you set leading-3
on both texts with different font sizes, the class sets line-height: .75rem
for texts. It's measured in rems, so calculated line-heights are different. Could you try setting a fixed line-height value on both like leading-[9px]
?
这可能是由于文本的字体大小不同造成的。当您在两个字体大小不同的文本上设置LEADING-3时,类将为文本设置行高:0.75rem。它是用雷姆测量的,所以计算的线高度是不同的。您是否可以尝试设置一个固定的行高值,如LEADING-[9px]?
优秀答案推荐
It does seems to me like the proper way, but it works. I had to set bigger text line-heigt
to 45px
and the smaller one to 15px
.
在我看来,这确实是正确的方式,但它奏效了。我不得不将较大的文本行高设置为45px,将较小的文本行高设置为15px。
<h2 className="text-font-color text-7xl
leading-[15px]">nothing but
</h2>
<h1 className="text-secondary text-9xl leading-[45px] ">the best</h1>
- Use
<span>
instead of <p>
- wrap both text in
<span>
hope this will work as you expected.
希望这将工作如你所料。
<div className="flex flex-col gap-4">
<div className="font-bebas bg-font-color ">
<h1 className="text-font-color text-4xl flex items-center flex-row gap-2">
<span>nothing but</span>
<span className="text-secondary text-5xl self-end">the best</span>
</h1>
</div>
<h2 className="text-secondary font-nautigal text-5xl">Frank Sinatra</h2>
</div>
更多回答
Good an idea, but it works only for text-4xl
and text-5xl
. Not for bigger font-size
好主意,但它只适用于Text-4XL和Text-5XL。不适用于更大的字体
我是一名优秀的程序员,十分优秀!