To use text in SVG, you use the ‘text’ label, and you can apply the same effects as to any other SVG element. Next, I’ve stressed the characteristics which I think are of most general interest but, for further detail you have to go to the specification at http://www.w3.org/TR/SVG/text.html.
The main characteristics of the text functioning in SVG are:
- Line breaks are not created automatically, instead you have to create them with the ‘tspan’ label inside the ‘text’ label or with several ‘text’ labels. This makes the use of html more recommendable in paragraphs with several lines.
- Texts can go on a straight line or adjust to a trace.
- Texts are accessible.
- You can use fonts with SVG fonts and webfonts.
The following example shows how the default text aligns on the baseline of the font. We can modify this behavior with the ‘alignment-baseline’ attribute.
1 2 3 4 5 6 |
<text x="5" y="5" font-family="Verdana" font-size="20" fill="skyblue"> Texto de prueba Á É Í Ó Ú </text> <text x="250" y="5" font-family="Verdana" font-size="20" alignment-baseline="text-before-edge" fill="skyblue"> Texto de prueba Á É Í Ó Ú </text> |
The ‘x’ and ‘y’ coordinates can have 1 or several values, separated by spaces or commas, affecting the text characters.
1 2 3 |
<text x="5,20,40,65,95,130,170,215,265,320" y="5" font-family="Verdana" font-size="25" alignment-baseline="text-before-edge" fill="skyblue"> aaaaaaaaaa </text> |
We can also change the relative position of the text with the ‘dx’ and ‘dy’ attributes, taking into account that the following text will keep this relative position.
1 2 3 |
<text font-family="Verdana" font-size="25" alignment-baseline="text-before-edge" fill="skyblue"> Texto de color <tspan dy="-10" alignment-baseline="text-before-edge" fill="red" >rojo</tspan> entre azul </text> |
With ‘textLength’ we can adjust the text to be adapted to the box size given by the value of the attribute which adjust the properties ‘x’, ‘y’, ‘kerning’, ‘letter-spacing’, ‘dx’ and ‘dy’.
1 2 3 4 |
<rect x="5" y="5" width="570" height="25" fill="white" stroke="grey" stroke-width="1"/> <text x="5" y="0" textLength="570" font-family="Verdana" font-size="25" alignment-baseline="text-before-edge" fill="skyblue"> Texto de prueba para textLength </text> |
To create new lines in the same text block, we’ll use the ‘tspan’ label with the ‘x’ and ‘y’ attributes.
1 2 3 4 5 6 7 8 |
<text x="5" y="5" font-family="Verdana" font-size="25" fill="skyblue"> <tspan alignment-baseline="text-before-edge"> Texto de prueba para 1 para x e y </tspan> <tspan x="5" y="35" alignment-baseline="text-before-edge"> Texto de prueba para 2 para x e y </tspan> </text> |
The ‘rotate’ attribute allow us to rotate the characters and it takes several values to rotate different letters. Child labels inherit the values but we can assign them new values.
1 2 3 4 5 6 |
<text x="5" y="5" rotate="-10" font-family="Verdana" font-size="25" alignment-baseline="text-before-edge" fill="skyblue"> Texto de prueba para rotación </text> <text x="5" y="35" rotate="-20,-10,0,10,20,30,40,50" font-family="Verdana" font-size="25" alignment-baseline="text-before-edge" fill="skyblue"> Texto de <tspan rotate="0" fill="red" alignment-baseline="text-before-edge">prueba</tspan> para rotación </text> |
We can index a text with the ‘tref’ label in a similar way to how we do with the graphic elements with the ‘use’ label.
1 2 3 4 5 6 7 8 9 |
<defs> <text id="ReferencedText" > Texto referenciado </text> </defs> <text x="5" y="5" font-family="Verdana" font-size="25" alignment-baseline="text-before-edge" fill="skyblue"> Texto no referenciado. <tref xlink:href="#ReferencedText" alignment-baseline="text-before-edge" fill="red" /> </text> |
The text, besides going in a straight line, can follow a path. To do that, you have to place a ‘textPath’ label with an ‘xlink:href’ attribute with the ‘id’ of the ‘path’.
1 2 3 4 5 6 |
<path x="5" y="5" id="MyPath" fill="none" d="M5,25c0,0,46.979,22.148,88.59,28.859s93.301-18.569,125.503-19.463c48.322-1.342,71.56,20.371,97.315,20.564c44.295,0.332,71.141-23.249,97.986-24.591c34.212-1.71,57.718,22.148,70.47,22.148s36.242-19.463,52.35-20.134" stroke="red" /> <text x="5" y="5" font-family="Verdana" font-size="20" alignment-baseline="text-before-edge" fill="skyblue"> <textPath xlink:href="#MyPath"> Texto en trazado, texto en trazado, texto en trazado, texto en trazado, texto en trazado, texto en trazado. </textPath> </text> |
Tags: SVG text