简体   繁体   English

在SVG路径中垂直/水平居中文本

[英]Vertically/Horizontally Center Text inside a SVG Path

I would like to vertically/horizontally center a text inside a rectangle made with a SVG Path element. 我想在一个用SVG Path元素制作的矩形内垂直/水平居中文本。

By center I don't mean having my first letter at the center of the rectangle but having the center of my text at the center of the path. 按中心我并不是说我的第一个字母位于矩形的中心,而是将文本的中心放在路径的中心。

Here is my code structure: 这是我的代码结构:

<svg id="shape">
    <path id = "a" d="M 0 0 L 100 0 L 100 100 L 0 100 Z"></path>
    <text>
        <textPath xlink:href="#a">My Text</textPath>
    </text>
</svg>

I've managed to achieve that by doing something like this: 我通过做这样的事情设法实现了这个目标:

<svg id="shape">
    <path id = "a" d="M 0 0 L 100 0 L 100 100 L 0 100 Z"></path>
    <text x="50" y="50" text-anchor="middle" alignement-baseline="middle">My Text</text>
</svg>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM