SVG Text Rotation Around the Center

Code Break picture Code Break · May 24, 2013 · Viewed 8.6k times · Source

I have the text with textContent "Design" and text-anchor as start, that was transformed with css to be rotated 45 degree. so it get rotated. problem is that i need to adjust the (x,y)position of the text after my rotation to display the text between the tick as shown in the Expected figure. How can i achieve this.

Result: http://imageshack.us/content_round.php?page=done&l=img404/2711/result1h.png

Expected: http://imageshack.us/content_round.php?page=done&l=img266/5138/expected1.png

    <svg>
    <text x="100" y="100" width="64" height="16" fill="black" transform="rotate(45,100,100)" text-anchor="start">Design</text>
    </svg>

Thanks Gowri

Answer

Mingwei Samuel picture Mingwei Samuel · May 4, 2015

The text needs to be centered horizontally and vertically so rotation won't move it:

<text x="100" y="50" text-anchor="middle"
    dominant-baseline="central" transform="rotate(0, 100, 50)"></text>

text-anchor aligns the text horizontally
dominant-baseline aligns the text vertically

svg {
    width: 200px; height: 100px;
    text-align: center;
    border: solid 1px blue;
    font-family: monospace;
}
<svg>
    <text x="100" y="50" text-anchor="middle" dominant-baseline="central" transform="rotate(180, 100, 50)">
       Hello World
   </text>
    <text x="100" y="50" text-anchor="middle" dominant-baseline="central" transform="rotate(0, 100, 50)">
       Hello World
   </text>
</svg>

example: http://jsfiddle.net/e4bAh/131/