Safari中的文本位置与其他浏览器中的位置不同

1.2*_*tts 8 html css safari webkit typography

Safari中的文本与其他浏览器中的文本位置不同.这有什么理由吗?是否有一种风格可确保其满足精确测量.

* {
  margin: 0;
  padding: 0;
}
#btn_signup {
  opacity: 1;
  position: absolute;
  width: 185px;
  height: 50px;
  left: 10px;
  top: 10px;
  overflow: visible;
}
#Rectangle_1 {
  opacity: 1;
  fill: transparent;
  stroke: rgb(67, 66, 93);
  stroke-width: 1px;
  stroke-linejoin: miter;
  stroke-linecap: butt;
  stroke-miterlimit: 4;
  shape-rendering: auto;
}
.Rectangle_1 {
  position: absolute;
  overflow: visible;
  width: 185px;
  height: 50px;
  left: 0px;
  top: 0px;
}
#Sign_up {
  opacity: 1;
  position: absolute;
  left: 58px;
  top: 16px;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  overflow: visible;
  width: 63px;
  white-space: nowrap;
  text-align: center;
}
#Path_1 {
  opacity: 1;
  fill: rgba(0,0,0,0);
  stroke: rgb(112, 112, 112);
  stroke-width: 0.5px;
  stroke-linejoin: miter;
  stroke-linecap: butt;
  stroke-miterlimit: 4;
  shape-rendering: auto;
}
.Path_1 {
  overflow: visible;
  position: absolute;
  top: 16.5px;
  left: 1px;
  width: 183.5px;
  height: 0.5px;
  transform: matrix(1,0,0,1,0,0);
}
Run Code Online (Sandbox Code Playgroud)
<div id="A5">
  <div id="btn_signup">
    <svg class="Rectangle_1">
      <rect id="Rectangle_1" rx="4" ry="4" x="0" y="0" width="185" height="50"></rect>
    </svg>
    <div id="Sign_up">
      <span style="font-family:Helvetica;font-style:normal;font-weight:normal;font-size:18px;color:rgba(77,79,92,1);display:inherit;">Sign up</span>
    </div>
    <svg viewBox="12.5 0 183.5 0.5" class="Path_1">
      <path id="Path_1" d="M 12.5 0 L 196 0"></path>
    </svg>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

在Firefox中,文本与行齐平.在Safari中有一些像素间隙,如下所示:

在此输入图像描述

如何消除这种差距?

增加了赏金.如果您需要更清晰,请提出问题.

更新:
感谢所有垂直居中的建议.我所拥有的一半案例将通过垂直居中来固定,但其余的是顶部对齐案例.

看起来我想要的是拥有我的文本内容,封面高度内容的边界框与文本行的上边缘对齐,如图所示.

资源:
看起来它是跨平台的常见问题.
封顶高度和x高度指标是不准确的
垂直度量标准

Jul*_*ire 5

请参阅此问题和答案: WebKit vs Mozilla Vertical Alignment of font glyphs in box

基本上,CSS 允许您调整文本元素的位置,但实际的字母位置与元素的关系取决于字体和浏览器。您无法直接修改它,这意味着从理论上讲,您无法真正确定字母本身的实际位置。

CSS 为您提供间接访问,例如使用Vertical-align,您可以将文本与父文本对齐。因此,根据值,元素将根据实际字母进行定位。例如:

div {
  font-size: 20px;
  line-height: 1.2;
  border-top: solid lightgray 1px;
  font-family: arial;
}

div span {
  display: inline-block;
  background-color: lightblue;
  border-top: solid red 1px;
}

.topalign span {
  vertical-align: top;
}

.bottomalign span {
  vertical-align: bottom;
}
Run Code Online (Sandbox Code Playgroud)
<div class="topalign">
  <span style="font-family: 'helvetica neue'; ">Helvetica N</span>
  <span style="font-family: 'arial'; font-size: 30px">Arial</span>
  <span style="font-family: 'Times new roman';  ">Times NR</span>
  <span style="font-family: 'Tahoma'; font-size: 15px;">Tahoma</span>
</div>
<div class="bottomalign">
  <span style="font-family: 'helvetica neue'; ">Helvetica N</span>
  <span style="font-family: 'arial'; font-size: 30px">Arial</span>
  <span style="font-family: 'Times new roman';  ">Times NR</span>
  <span style="font-family: 'Tahoma'; font-size: 15px;">Tahoma</span>
</div>
Run Code Online (Sandbox Code Playgroud)

您还可以使用line-height,这将更改文本元素的高度而不更改字体大小,这意味着您可以或多或少地控制要放置字母的位置。但同样,不同的字体/浏览器会呈现不同的效果。您仍然没有精确定位字母。例如:

div {
  font-size: 30px;
  line-height: 1.2;
  border-top: solid lightgray 1px;
  font-family: arial;
}

div span {
  display: inline-block;
  background-color: lightblue;
  border-top: solid red 1px;
}

.topalign span {
  vertical-align: top;
}
Run Code Online (Sandbox Code Playgroud)
<div class="topalign">
  <span style="font-family: 'helvetica neue'; ">Helvetica N</span>
  <span style="font-family: 'arial'; line-height: 1.5;">Arial</span>
  <span style="font-family: 'Times new roman';  line-height: 0.5;">Times NR</span>
  <span style="font-family: 'Tahoma'; ">Tahoma</span>
</div>
Run Code Online (Sandbox Code Playgroud)

因此从理论上讲,您无法精确地将实际字母位置定义在您想要的位置。它不能是明确的,因此可能导致跨浏览器的不一致。

话虽这么说,你指出的情况似乎是 Firefox Helvetica 字体的错误。Helvetica 的字母位置与其他字体或其他浏览器不一致。当你与Helvetica NeueArial进行比较时,这一点尤其明显,它们应该或多或少是相同的。看:

div {
  font-size: 30px;
  line-height: 1.2;
  border-top: solid lightgray 1px;
  font-family: arial;
}

div span {
  display: inline-block;
  background-color: lightblue;
  border-top: solid red 1px;
}

.topalign span {
  vertical-align: top;
}
Run Code Online (Sandbox Code Playgroud)
<div class="topalign">
  <span style="font-family: 'helvetica';  ">Helvetica </span>
  <span style="font-family: 'helvetica neue'; ">Helvetica N</span>
  <span style="font-family: 'arial'; ">Arial</span>
</div>
Run Code Online (Sandbox Code Playgroud)

即使您无法明确说明字母的位置,您仍然可以期望一定程度的一致性。通常,顶部和底部有一个正常且连贯的填充,或多或少相等。它可能因字体、浏览器和浏览器而异,但 Firefox 中呈现 helvetica 的方式确实看起来像是一个错误(至少对我来说,也许有一个原因,但我不明白为什么)。

因此,如果您可以将Helvetica替换为Helvetica Neue,则可以使用line-height来定位字母与元素的位置并实现您想要的效果。例如,通常大约 0.75 的行高将与大写字母的顶部和底部齐平,这意味着通过定位元素,您可以定位字母。例如这样:

* {
  margin: 0;
  padding: 0;
}
#btn_signup {
  opacity: 1;
  position: absolute;
  width: 185px;
  height: 50px;
  left: 10px;
  top: 10px;
  overflow: visible;
}
#Rectangle_1 {
  opacity: 1;
  fill: transparent;
  stroke: rgb(67, 66, 93);
  stroke-width: 1px;
  stroke-linejoin: miter;
  stroke-linecap: butt;
  stroke-miterlimit: 4;
  shape-rendering: auto;
}
.Rectangle_1 {
  position: absolute;
  overflow: visible;
  width: 185px;
  height: 50px;
  left: 0px;
  top: 0px;
}
#Sign_up {
  opacity: 1;
  position: absolute;
  left: 58px;
  top: 16px;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  overflow: visible;
  width: 63px;
  white-space: nowrap;
  text-align: center;
}
#Path_1 {
  opacity: 1;
  fill: rgba(0,0,0,0);
  stroke: rgb(112, 112, 112);
  stroke-width: 0.5px;
  stroke-linejoin: miter;
  stroke-linecap: butt;
  stroke-miterlimit: 4;
  shape-rendering: auto;
}
.Path_1 {
  overflow: visible;
  position: absolute;
  top: 16.5px;
  left: 1px;
  width: 183.5px;
  height: 0.5px;
  transform: matrix(1,0,0,1,0,0);
}
Run Code Online (Sandbox Code Playgroud)
<div id="A5">
  <div id="btn_signup">
    <svg class="Rectangle_1">
      <rect id="Rectangle_1" rx="4" ry="4" x="0" y="0" width="185" height="50"></rect>
    </svg>
    <div id="Sign_up">
      <span style="font-family:Helvetica neue;font-style:normal;font-weight:normal;font-size:18px;color:rgba(77,79,92,1);display:inherit;line-height: 0.75;">Sign up</span>
    </div>
    <svg viewBox="12.5 0 183.5 0.5" class="Path_1">
      <path id="Path_1" d="M 12.5 0 L 196 0"></path>
    </svg>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)