svg - 使用文本 anchor : middle and alignment-baseline: middle/central 在 IE11 上居中 SVG 文本

标签 svg internet-explorer-11

我需要使用 IE11 在 SVG 中完美地居中文本元素。

预期结果(所有 Evergreen 浏览器):
enter image description here

IE11 结果:
enter image description here

//SVG 代码:

<svg xmlns="http://www.w3.org/2000/svg" width=100% height="100%" viewBox="0 0 130 130">
    <circle fill="dodgerblue" cx="50%" cy="50%" r="65"></circle>
    <text text-anchor="middle"
        alignment-baseline="central"
        font-size="75"
        font-family="Arial"
        x="50%" y="50%">1</text>
  </svg>

Codepen

最佳答案

IE 不支持 alignment-baseline .实现您想要的最佳跨浏览器方式是使用 dy带小数的属性 em值(value)。

值约为 dy="0.35"为 Arial 工作。

<svg xmlns="http://www.w3.org/2000/svg" width=100% height="100%" viewBox="0 0 130 130">
  <circle fill="dodgerblue" cx="50%" cy="50%" r="65"></circle>
  <text text-anchor="middle"
        font-size="75"
        font-family="Arial"
        x="50%" y="50%" dy="0.35em">1</text>
</svg>

关于svg - 使用文本 anchor : middle and alignment-baseline: middle/central 在 IE11 上居中 SVG 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48831606/

相关文章:

javascript - 脚本438 : Object doesn't support property or method 'addObject'

javascript - 为什么 IE11 在 onkeyup 中设置文本框值时按 Ctrl+Z 时会重复内容?

javascript - 如何使用 SVG.invent() 创建自定义 svg.js 元素?

css - 将省略号添加到 SVG 中的溢出文本?

javascript - 使用带有 ScrollMagic 的 SVG 动画时出现奇怪的点

javascript - IE 11 中的 &lt;input type ="file"> 属性未正确显示

javascript - IE 11 Script1002 过滤器语法错误

jquery - 动画 div 不会隐藏在父项下

javascript - DOM 节点清理在 d3 中如何工作?

html - IE11 损坏或溢出的边框/轮廓