如何将两个DOM图层与文本对齐?

时间:2018-05-01 12:50:59

标签: html css

我想要实现的是将一个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>

2 个答案:

答案 0 :(得分:3)

我怀疑这是因为某个字体如何处理特定字符/字形组合之间的连字和字距。在您的背景中,角色都被分解为单个单词,因为您将它们包装在单独的<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>

答案 1 :(得分:2)

使用font-kerning:none;解决问题。在大多数情况下,font-kerning:none;甚至不会引起注意,除非是非常勤奋的设计师,他们非常注重字体。