如何在 SVG 圆圈内完美地居中单个字符? [英] How do I perfectly center a single character inside an SVG circle?
本文介绍了如何在 SVG 圆圈内完美地居中单个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在这个圆圈内放置一个完全居中的字符:
I'd like to place a single character, perfectly centered, inside this circle:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" width="100" height="100">
<g>
<circle style="fill:#eeeeee" cx="50" cy="50" r="50">
</circle>
<text>C</text>
</g>
</svg>
理想情况下,该解决方案适用于任何单个 ASCII 字符.
Ideally, the solution works for any single ASCII character.
感谢您的帮助!
推荐答案
使用 text-anchor="middle"
使文本水平居中,dominant-baseline="central"
使其垂直居中.
Use a combination of text-anchor="middle"
to centre the text horizontally, and dominant-baseline="central"
to centre it vertically.
为了简化事情,我在 <g>
元素中添加了一个 transform
属性,以将原点移动到画布的中间.
To simplify things, I've added a transform
attribute to your <g>
element to move the origin to the middle of your canvas.
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100">
<g transform="translate(50,50)">
<circle style="fill:#eeeeee" r="50" />
<text text-anchor="middle" dominant-baseline="central">C</text>
</g>
</svg>
这篇关于如何在 SVG 圆圈内完美地居中单个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文