Background color of tspan element

A rect is probably the best way to do that (assuming you are only dealing with the simplest forms of text).

The tspans have no “background” themselves in SVG 1.1, the background is whatever gets painted before the tspan. There are many cases to consider, such as the case where a tspan is inside a textPath that has the shape of a circle. Also note that it’s not as simple as a continous rectangle in all cases, a single tspan can be skewed, rotated and partitioned into several intersecting and non-intersecting shapes due to transforms, glyph positioning, etc.

There’s another way I can think of that would do this without scripting, but then you’d need to know the width of the string in advance (e.g by using a monospaced font). If you have that then you can add another tspan element using the Ahem font, placing it before the other tspan in the document and giving it the same x,y position as the tspan you want to give a “background”.

Otherwise the way to do this is through scripting, and adding rectangles (or tspans with an Ahem-like font).

Leave a Comment