JSmith
JSmith

Reputation: 4810

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.

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>

Upvotes: 2

Views: 2484

Answers (1)

JSmith
JSmith

Reputation: 4810

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>

Upvotes: 1

Related Questions