Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set a svg's <text>/<tspan> to a fixed width?

Tags:

css

svg

I want to align a text to the right and in this case I have to make the text in a fixed width. Because the text content is dynamically added.

<text>
  <tspan x="421" y="15" text-anchor="right"
     baseline-shift="0%" kerning="0" 
     font-family="Arial" font-weight="bold"
     font-size="12" fill="#490275" xml:space="preserve">
       This is entered by user.
</tspan>
</text>
like image 362
AGamePlayer Avatar asked Feb 02 '17 13:02

AGamePlayer


1 Answers

I think your attempt is close, you're just using the wrong value for text-anchor. If you use text-anchor="end" it will align the text to the right of the element.

So, we can set the x position of the tspan to 100%, and along with text-anchor="end" the text will be positioned to the right regardless of length.

<svg width="100%" height="110">
  <text>
  <tspan x="100%" y="15" text-anchor="end"
     baseline-shift="0%" kerning="0" 
     font-family="Arial" font-weight="bold"
     font-size="12" fill="#490275" xml:space="preserve">
       This is entered by user.
</tspan>
</text>
  Sorry, your browser does not support inline SVG.  
</svg>
like image 126
Brett DeWoody Avatar answered Oct 17 '22 08:10

Brett DeWoody