提问者:小点点

如何在Firefox上的svg元素中居中设置文本?


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

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

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

如何让它处处发挥作用?

(还有,如果字体对齐不好,有没有办法移动几个像素?边距或顶部/底部似乎都不起作用)


共1个答案

匿名用户

因此,根据Mozilla文档,标记中不支持“alignation-baseline”和“text-anchor”,因此您必须使用x和y位置。 (链接)

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

<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>

不过,你可能还可以多花点时间来让它完全按你想要的方式集中。