Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert text between a rectangle drawn in SVG

I have a code in SVG:

<svg width="100%" height="100%" version="1.1"xmlns="http://www.w3.org/2000/svg">
    <rect x="20" y="20" width="250" height="250" style="fill:blue">
        <animate attributeType="CSS" attributeName="opacity" from="1" to="0" dur="5s" repeatCount="indefinite" />
    </rect>
</svg>

Now I need to add a text between this rectangle. Can anyone tell me how to do it?

like image 582
Maverick Avatar asked Mar 27 '11 14:03

Maverick


People also ask

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 ).

How do I put text in SVG in HTML?

SVG Text - <text> The <text> element is used to define a text.


1 Answers

I am not sure what you mean by "between". If you mean "centered horizontally and vertically", then this would do it:

<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
  <rect x="20" y="20" width="250" height="250" style="fill:blue" />
  <text x="145" y="145" text-anchor="middle" alignment-baseline="middle">
    Hello World
  </text>
</svg>

Did you mean something else?

If you are talking about having text filling the rectangle—multiple lines of text wrapping at the edges of the rectangle onto a new line—then you should see this Stack Overflow question instead.

like image 51
Phrogz Avatar answered Oct 19 '22 06:10

Phrogz