html - CSS:基于基线的绝对位置文本?

标签 html css

我有需要在页面上绝对定位的文本坐标,但我有的坐标是文本的基线,而不是文本的左上角(可以是任何内容,具体取决于字符。)

目前文本会定位在左上角,这使得我的文本无法准确定位。

如何设置文本基线的绝对位置?也就是说,我要指定文本的基线位置,而不是文本的顶部。

请参阅:https://jsfiddle.net/8hnjoo68/

<div style="position:absolute; top:0px; left:0px; font-size: 42px;">This text should be off the page because it's baseline is set at 0.</div>
<div style="position:absolute; top:42px; left:0px; font-size: 42px;">Second line of text should be where the first one is, more or less.</div>

最佳答案

您需要将两个 div 包装在具有相对定位的 div 中。这样,当您设置 bottom: 0 时,它实际上会相对于 div 移动,让您产生消失的错觉。

示例 CodePen

请注意,您可能会看到一些悬挂的字母。要对此进行调整,您只需向 bottom: 20px;

添加更多内容
<div style="position:relative;">
    <div style="position:absolute; bottom: 0; left:0px; font-size: 42px;">This text should be off the page because it's baseline is set at 0.</div>
    <div style="position:absolute; left:0px; font-size: 42px;">Second line of text should be where the first one is, more or less.</div>
</div>

关于html - CSS:基于基线的绝对位置文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33360744/

相关文章:

php - 我如何制作幻灯片,从数据库中获取信息(图像、标题、内容)?

html - CSS div 椭圆形

javascript - 如何从表中动态删除/编辑行和列

html - 表格标签占用的高度高于父 div 提到的高度

html - 使用 :hover using CSS 添加类

html - 奇数空白 : pre behavior inside floated parent

javascript - ASP.NET UserControl javascript 未将 HTML 加载到其变量

html - 尽管设置了 z 索引,div 仍位于其他 div 之下

html - Solr 响应中的内容类型

用于 CSS 属性的 Jquery 轻松更改并再次返回原位