如何垂直对齐两种不同大小的文本?

发布于 2024-09-28 08:12:32 字数 181 浏览 6 评论 0原文

我知道要将文本垂直对齐到块的中间,您可以将行高设置为块的相同高度。

但是,如果我有一个句子中间有一个单词,那就是2em。如果整个句子的行高与包含块相同,则较大的文本垂直对齐,但较小的文本与较大的文本位于同一基线上。

如何设置才能使两种大小的文本垂直对齐,以便较大的文本的基线低于较小的文本?

I know to vertically align text to the middle of a block, you set the line-height to the same height of the block.

However, if I have a sentence with a word in the middle, that is 2em. If the entire sentence has a line-height the same as the containing block, then the larger text is vertically aligned but the smaller text is on the same baseline as the larger text.

How can I set it so both sizes of text are vertically aligned, so the larger text will be on a baseline lower than the smaller text?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

锦欢 2024-10-05 08:12:32

在内联容器上尝试 vertical-align:middle; 吗?

编辑:它可以工作,但所有文本都必须位于内联容器中,如下所示:

    <div style="height:100px; line-height:100px; background:#EEE;">
        <span style="vertical-align:middle;">test</span>
        <span style="font-size:2em; vertical-align:middle;">test</span>
    </div>

Try vertical-align:middle; on inline containers?

EDIT : it works but all your text must be in an inline container, like this :

    <div style="height:100px; line-height:100px; background:#EEE;">
        <span style="vertical-align:middle;">test</span>
        <span style="font-size:2em; vertical-align:middle;">test</span>
    </div>

十级心震 2024-10-05 08:12:32

两组文本必须具有相同的固定行高和垂直对齐集

 span{
     vertical-align: bottom;
     line-height: 50px;
}

the two set of text must have the same fixed line-height and the vertical-align set

 span{
     vertical-align: bottom;
     line-height: 50px;
}
别在捏我脸啦 2024-10-05 08:12:32

您看到的功能是正确的,因为“垂直对齐”的默认值是基线。看来您需要 vertical-align:top。还有其他选择。
请参阅此处 W3Schools

编辑 W3Schools 尚未整顿他们的行为,而且看起来仍然是一个劣质(充其量)的信息来源。我现在使用 sitepoint。滚动到 sitepoint 首页的底部以访问其参考部分。

The functionality you are seeing is correct because the default for "vertical-align" is baseline. It appears that you want vertical-align:top. There are other options.
See here at W3Schools.

Edit W3Schools has not cleaned up their act and still, appear, to be a shoddy (at best) source of information. I now use sitepoint. Scroll to the bottom of the sitepoint front page to access their reference sections.

陈甜 2024-10-05 08:12:32

简单的方法 - 使用 Flex:

<div>
        abcde
          
        <span>efghai</span>
</div>

<style>
    div {
        padding: 20px;
        background-color: orange;
        display: flex;
        align-items: center; }

    span {
        font-size: 1.5em; }
</style>

Easy way - use flex:

<div>
        abcde
          
        <span>efghai</span>
</div>

<style>
    div {
        padding: 20px;
        background-color: orange;
        display: flex;
        align-items: center; }

    span {
        font-size: 1.5em; }
</style>
那些过往 2024-10-05 08:12:32

但是,从技术上讲,如果您有固定的文本大小,您可以使用定位(相对)将较大的文本向下移动并将行高设置为较小的文本(我假设这个较大的文本被标记为这样)所以你可以使用它作为 CSS 选择器)

You technically can't, however, if you have fixed text sizes you could use positioning (relative) to move the larger text down and set the line-height to the smaller text (I'm presuming this larger text is marked up as such so you can use that as a CSS selector)

嗳卜坏 2024-10-05 08:12:32

您可以使用百分比大小来重新应用父级的 line-height

.big {
  font-size: 200%;
  line-height: 25%;
  display: inline-block;
  vertical-align: middle;
}
Utque aegrum corpus <span class="big">etiam</span> levibus solet offensis 

You can use percentage sizes to reapply the parent's line-height

.big {
  font-size: 200%;
  line-height: 25%;
  display: inline-block;
  vertical-align: middle;
}
Utque aegrum corpus <span class="big">etiam</span> levibus solet offensis 

无法言说的痛 2024-10-05 08:12:32

一种选择是使用表格,其中不同大小的文本位于各自的单元格中,并在每个单元格上使用align:middle。

它并不漂亮,并且不适用于所有场合,但它很简单,并且适用于任何文本大小。

An option is to use a table there the different sized texts are in their own cells and use align:middle on each cell.

Its not pretty and does not work for all occasions, but it is simple and works with any text size.

凉宸 2024-10-05 08:12:32

这有效

header > span {
    margin: 0px 12px 0px 12px;
    vertical-align:middle;
}
.responsive-title{
    font-size: 12vmin;
    line-height: 1em;
}
.responsive-subtitle{
    font-size: 6vmin;
    line-height: 2em;
}
<header>
  <span class="responsive-title">Foo</span>
  <span class="responsive-subtitle">Bar</span>
</header>

This works

header > span {
    margin: 0px 12px 0px 12px;
    vertical-align:middle;
}
.responsive-title{
    font-size: 12vmin;
    line-height: 1em;
}
.responsive-subtitle{
    font-size: 6vmin;
    line-height: 2em;
}
<header>
  <span class="responsive-title">Foo</span>
  <span class="responsive-subtitle">Bar</span>
</header>

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