我需要在SVG对象内水平居中文本,而无需定义x坐标。我正在尝试寻找CSS中text-align: center的替代方法。我已经使用了text-anchor: middle,但它不起作用。以下是我的代码:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0" stop-color="#FFE89C" stop-opacity="1"/>
<stop offset="0.2" stop-color="#FFE192" stop-opacity="1"/>
<stop offset="0.4" stop-color="#ffd47a" stop-opacity="1"/>
<stop offset="0.6" stop-color="#ffc967" stop-opacity="1"/>
<stop offset="0.8" stop-color="#febd52" stop-opacity="1"/>
<stop offset="1" stop-color="#fdba4c" stop-opacity="1"/>
</linearGradient>
</defs>
<text fill="url(#grad1)" x="73" y="50">50</text>
</svg>
text-anchor: middle
是正确的方法。实际上没有其他方法可以做到。你说的“它不起作用”是什么意思? - Paul LeBeau