【问题标题】:Svg shows different on Firefox and EdgeSVG 在 Firefox 和 Edge 上显示不同
【发布时间】:2017-11-16 14:52:18
【问题描述】:

我在处理中间有数字的 svg 图像时遇到了一些问题。使用 Chrome 和其他浏览器时显示正确,但在 Firefox 和 Edge 中,数字位置错误。 Link

这是我的代码。

#maindiv {
  position: relative;
  width: 50px;
  height: 50px;
}
.number {
  font-family: initial;
  font-size: 2.5em;
  font-weight: 700;
  text-align: center;
}

<div id="maindiv" class="" style="">
<svg viewBox="0 -10 50 90" enable-background="new 0 0 50 50">
  <defs>
    <!-- Background image for the svg -->
    <pattern id="image_594121ec06330" patternUnits="userSpaceOnUse" height="50" width="50" x="0" y="20">
      <image x="0" y="0" height="50" width="50" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://via.placeholder.com/50x50">
      </image>
    </pattern>

    <!-- two colors for the number -->
    <linearGradient id="textcolor_594121ec06343" x1="0%" y1="0%" x2="0%" y2="100%">
      <stop offset="50%" stop-color="#ff0100"></stop>
      <stop offset="50%" stop-color="#0600c4"></stop>
      <stop offset="50%" stop-color="#0600c4"></stop>
      <stop offset="0" stop-color="#0600c4"></stop>
    </linearGradient>
  </defs>
  <rect class="cellImage" id="top" x="0" y="20" width="50" height="50" style="" fill="url(#image_594121ec06330)"></rect>
  <text class="number " x="50%" y="55%" alignment-baseline="middle" text-anchor="middle" fill="url(#textcolor_594121ec06343)">0</text>
</svg>

这是 jsfiddle 上的相同代码 https://jsfiddle.net/41s39p63/

我知道,如果用户使用的是 Firefox 或 Edge,但我不喜欢使用该解决方案,我知道可以通过更改某些 svg 属性以将数字放在中间的一些 if 条件来解决此问题。

有没有人可以帮助我解决这个问题,使其适用于所有浏览器?

【问题讨论】:

    标签: firefox svg microsoft-edge


    【解决方案1】:

    Firefox 不支持对齐基线。您应该能够使用显性基线来获得相同的效果。

    【讨论】:

    • 谢谢,它解决了位置,但它弄乱了数字的颜色。
    • 主导基线对着色没有影响。您的渐变停止偏移不按顺序排列,所以这可能是个问题。
    • 我的回答并没有说它会,不是吗?如果你想支持 Edge,你可能需要用 dy 手动做一些事情。 IE。可能根本没有简单的 Edge 兼容解决方案。
    • 我也遇到了dominant-baseline 的问题。
    猜你喜欢
    • 2017-01-12
    • 2021-11-02
    • 1970-01-01
    • 2020-07-31
    • 2015-09-26
    • 2020-02-06
    • 2018-07-10
    • 2014-03-06
    相关资源
    最近更新 更多