如何垂直对齐两种不同大小的文本?
我知道要将文本垂直对齐到块的中间,您可以将行高设置为块的相同高度。
但是,如果我有一个句子中间有一个单词,那就是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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在内联容器上尝试
vertical-align:middle;
吗?编辑:它可以工作,但所有文本都必须位于内联容器中,如下所示:
Try
vertical-align:middle;
on inline containers?EDIT : it works but all your text must be in an inline container, like this :
两组文本必须具有相同的固定行高和垂直对齐集
the two set of text must have the same fixed line-height and the vertical-align set
您看到的功能是正确的,因为“垂直对齐”的默认值是基线。看来您需要
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.
简单的方法 - 使用 Flex:
Easy way - use flex:
但是,从技术上讲,如果您有固定的文本大小,您可以使用定位(相对)将较大的文本向下移动并将行高设置为较小的文本(我假设这个较大的文本被标记为这样)所以你可以使用它作为 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)
您可以使用百分比大小来重新应用父级的
line-height
You can use percentage sizes to reapply the parent's
line-height
一种选择是使用表格,其中不同大小的文本位于各自的单元格中,并在每个单元格上使用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.
这有效
This works