This answer and this answer explain how to show multiple lines of text and how to center one line of text with SVG, but how do you center multiple lines of text?
As you can see from this Code Pen, the text block is not centered because of the dy
attribute, which is needed to display multiply lines.
The goal is to allow dynamic insertion/deletion of lines while preserving the centered nature of the text block. So the user might add a fourth line or delete two lines. In both cases, the text block should remain centered.
One approach is to modify the dy
values each time a line is removed/inserted as some suggested, but is there a non-JS approach to vertically centering a block of text?
<svg style="border:1px solid black" width="200" height="300">
<text x="50%" y="50%" font-size="15">
<tspan x="0" dy="1.2em" dominant-baseline="central">tspan line 1</tspan>
<tspan x="0" dy="1.2em" dominant-baseline="central">tspan line 2</tspan>
<tspan x="0" dy="1.2em" dominant-baseline="central">tspan line 3</tspan>
</text>
</svg>