Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align text center below a SVG circle?

Tags:

html

svg

Suppose I have the following SVG:

<g transform="translate(300, 300)">
    <circle r="5px"></circle>
    <text>My Label</text>
</g>

I need the label to be centered below the circle. Is there a simple solution to this problem?

like image 209
user1405177 Avatar asked Feb 19 '14 17:02

user1405177


People also ask

How do I center text in a circle SVG?

Add text-anchor="middle" to the text element. Works good!

How do I center a circle in SVG?

As you've probably noticed in the SVG above, the attributes CX , CY , and R respectively define where the circle is drawn along the X and Y axis, while R defines the radius of the circle. The CX and CY create the center of the circle, so the circle is drawn around that point.

How do I center SVG content?

How Do I Center An Svg In Svg? the svg as follows: You can either add this style = “text-align center:” to the div in order to insert the center text in the div, or you can assign this style = “display:block”; margin; auto”; that style = “display: block”; .

How do I put text inside an SVG rectangle?

If you want to display text inside a rect element you should put them both in a group with the text element coming after the rect element ( so it appears on top ). The group fits to its content not the other way.


1 Answers

One option:

<g transform="translate(300, 300)">
    <circle r="5px"></circle>
    <text baseline-shift="-20px" text-anchor="middle">My Label</text>
</g>

The -20px depends on your font size, and maybe someone has a relative way of doing the drop, but the text-anchor="middle" will center the text.

like image 169
Jason Aller Avatar answered Sep 26 '22 03:09

Jason Aller