gpt4 book ai didi

html - 如何将两个 DOM 层与彼此重叠的文本对齐?

转载 作者:太空宇宙 更新时间:2023-11-04 14:51:36 24 4
gpt4 key购买 nike

我想要实现的是将一个 DOM 层(背景)对齐到另一个(前景)之上。两个层都包含完全相同的文本。

前景只是纯文本。

背景 包含相同文本的 span

出于某种原因,只有空格的 span 打乱了对齐方式。我想这与 white-space CSS 属性有关。

我错过了什么?

.foreground,
.background {
top: 0;
width: 400px;
line-height: 1.5;
font-size: 18px;
position: absolute;
white-space: pre-wrap;
word-wrap: break-word;
letter-spacing: 1px;
}

.foreground {
z-index: 2;
}

.background {
z-index: 1;
color: red;
opacity: 0.4;
}

.color {
border-bottom: 1px solid green;
}
<div class="wrapper">
<div class="foreground">‘Holy cow, he’s right, probably 90 percent of the stories involve aliens.'</div>
<div class="background"><div class="section"><span class="space"><span>‘</span></span><span class="color"><span>Holy</span></span><span class="space"><span> </span></span><span class="color"><span>cow</span></span><span class="space"><span>, </span></span><span class="color"><span>he’s</span></span><span class="space"><span> </span></span><span class="color"><span>right</span></span><span class="space"><span>, </span></span><span class="color"><span>probably</span></span><span class="space"><span> 90 </span></span><span class="color"><span>percent</span></span><span class="space"><span> </span></span><span class="color"><span>of</span></span><span class="space"><span> </span></span><span class="color"><span>the</span></span><span class="space"><span> </span></span><span class="color"><span>stories</span></span><span class="space"><span> </span></span><span class="color"><span>involve</span></span><span class="space"><span> </span></span><span class="color"><span>aliens</span></span><span class="space"><span>.'</span></span></div></div>
</div>

最佳答案

我怀疑这是因为某种字体如何处理连字和字距调整——特定字符/字形组合之间的间距。在您的背景中,字符都被分解成单独的单词,因为您将它们包装在单独的 <span> 中。元素。这使得浏览器呈现字形之间的空白略有不同。

当您注意到第一个“偏移量”恰好位于第一个 , 的位置时,这个假设就有意义了。字符被引入:字体渲染可能知道它应该在单词和尾随逗号之间使用稍微不同的间距,这会导致差异发生:

enter image description here

另一个证明是使用等宽字体,你可以看到你的问题消失了:

.foreground,
.background {
top: 0;
width: 400px;
line-height: 1.5;
font-size: 18px;
font-family: monospace;
position: absolute;
white-space: pre-wrap;
word-wrap: break-word;
letter-spacing: 1px;
}

.foreground {
z-index: 2;
}

.background {
z-index: 1;
color: red;
opacity: 0.4;
}

.color {
border-bottom: 1px solid green;
}
<div class="wrapper">
<div class="foreground">‘Holy cow, he’s right, probably 90 percent of the stories involve aliens.'</div>
<div class="background"><div class="section"><span class="space"><span>‘</span></span><span class="color"><span>Holy</span></span><span class="space"><span> </span></span><span class="color"><span>cow</span></span><span class="space"><span>, </span></span><span class="color"><span>he’s</span></span><span class="space"><span> </span></span><span class="color"><span>right</span></span><span class="space"><span>, </span></span><span class="color"><span>probably</span></span><span class="space"><span> 90 </span></span><span class="color"><span>percent</span></span><span class="space"><span> </span></span><span class="color"><span>of</span></span><span class="space"><span> </span></span><span class="color"><span>the</span></span><span class="space"><span> </span></span><span class="color"><span>stories</span></span><span class="space"><span> </span></span><span class="color"><span>involve</span></span><span class="space"><span> </span></span><span class="color"><span>aliens</span></span><span class="space"><span>.'</span></span></div></div>
</div>

不幸的是,您的问题没有直接的解决方案,因为单词和尾随逗号之间的间距似乎不受 font-variant-ligatures: none; 的影响。声明。我的建议是,您可能只想在背景和前景元素中镜像标记。

您可以强制浏览器禁用自定义字体字距调整,这应该可以解决问题。这可以通过使用 font-kerning: none 来完成。 (感谢@Kashif Imran 在评论中指出):

.foreground,
.background {
top: 0;
width: 400px;
line-height: 1.5;
font-size: 18px;
font-kerning: none;
position: absolute;
white-space: pre-wrap;
word-wrap: break-word;
letter-spacing: 1px;
}

.foreground {
z-index: 2;
}

.background {
z-index: 1;
color: red;
opacity: 0.4;
}

.color {
border-bottom: 1px solid green;
}
<div class="wrapper">
<div class="foreground">‘Holy cow, he’s right, probably 90 percent of the stories involve aliens.'</div>
<div class="background"><div class="section"><span class="space"><span>‘</span></span><span class="color"><span>Holy</span></span><span class="space"><span> </span></span><span class="color"><span>cow</span></span><span class="space"><span>, </span></span><span class="color"><span>he’s</span></span><span class="space"><span> </span></span><span class="color"><span>right</span></span><span class="space"><span>, </span></span><span class="color"><span>probably</span></span><span class="space"><span> 90 </span></span><span class="color"><span>percent</span></span><span class="space"><span> </span></span><span class="color"><span>of</span></span><span class="space"><span> </span></span><span class="color"><span>the</span></span><span class="space"><span> </span></span><span class="color"><span>stories</span></span><span class="space"><span> </span></span><span class="color"><span>involve</span></span><span class="space"><span> </span></span><span class="color"><span>aliens</span></span><span class="space"><span>.'</span></span></div></div>
</div>

关于html - 如何将两个 DOM 层与彼此重叠的文本对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50116807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com