Is it possible to display multiple lines of text in SVG without using the dy
property? I'm using SVG 1.1 but might be able to use 1.2.
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<text x="0" y="15" font-size="15">
<tspan>tspan line 1</tspan>
<tspan>tspan line 2</tspan>
<tspan>tspan line 3</tspan>
</text>
</svg>
I've typed the code above. I want the text all flush to the left and each tspan
to be a new line. Is tspan
the only tag I can use? I want SVG to position the text lines vertically with line breaks. I do not want to manually enter the dy
.
According to what I've read, each line should appear below the other. They are but they are also staggered across.
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<text x="0" y="0" font-size="15">
<tspan dy="15">tspan line 1</tspan>
<tspan dy="15">tspan line 2</tspan>
<tspan dy="15">tspan line 3</tspan>
</text>
</svg>
I guess it is required to add the x
property. If you are setting the dy
property to a fixed value, what happens when you change the font size?
This is working better than what I started with:
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xml:space="preserve">
<text x="0" y="0" font-size="15" font-family="courier new" dy="0">
<tspan x="0" dy="15">tspan line 1</tspan>
<tspan x="0" dy="15">tspan line 2</tspan>
<tspan x="0" dy="15">tspan line 3</tspan>
</text>
</svg>
Is there a way to apply the x
and dy
to all the tspan
s? Maybe a property like line-height
on the text
element?
It doesn't look like the text tag has a property to set the delta y. It has been suggested in the comments to use JQuery to set the x
attribute of all tspan
s. It looks like the dy
property can accept other types of values such as points and percentages!? Is there a way to set the dy
to a value that is 120% of the font size of its parent text element? I've tried to set it to 120%, but it doesn't seem to work like I expect. When I set 120% in the dy
property, it goes off the screen. When I set it to 12
or 12px
it stays in the same position. If I set it to 12%
, it shifts slightly but is not 120% or 12px.
http://codepen.io/anon/pen/PqBRmd
It looks like it can accept any of the following:
http://www.w3.org/TR/SVG/types.html#InterfaceSVGLength
I've also looked up acceptable value types for dy
and dx
, and I can't make sense of it http://www.w3.org/TR/SVG/text.html#TSpanElementDXAttribute.
UPDATE 4:
Thanks for the answers so far. It looks like there is a way to display multiple lines of text spaced apart relatively. See my answer below.