【问题标题】:How to center text inside an svg element on Firefox?如何在 Firefox 的 svg 元素中居中文本?
【发布时间】:2020-06-23 23:23:41
【问题描述】:

在我的 svg 元素中,我有这个文本元素:

<text alignment-baseline="middle" text-anchor="middle">

这在 Chrome 上垂直居中元素,但在 Firefox 上失败:https://jsfiddle.net/g398ajc0/1/

如何让它无处不在?

(如果字体没有很好地对齐,还有什么方法可以将它移动几个像素?边距或顶部/底部似乎不起作用)

【问题讨论】:

    标签: css svg


    【解决方案1】:

    因此,根据 Mozilla 文档,标签中不正式支持“alignment-baseline”和“text-anchor”,因此您必须改用 x 和 y 位置。 (Link)

    所以我发现看起来最好的是:

    <svg viewBox="-20,-20,40,40" class="progress-ring">
        <circle r="20" class="progress-ring-circle"></circle>
        <path d="M0,-20 A20,20,0,0,1,6.180339887498949,-19.02113032590307" 
        class="progress-ring-ring" style="stroke: rgb(255, 0, 0);"></path>
        <text dx="-4" dy="5"> 1 </text>
    </svg>
    

    不过,您可能会多花点功夫,让它完全以您想要的方式居中。

    【讨论】:

      猜你喜欢
      • 2022-10-30
      • 1970-01-01
      • 2019-05-18
      • 2017-10-03
      • 2021-10-23
      • 2013-05-13
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      相关资源
      最近更新 更多