Can I set the background color of an SVG <tspan> element? If not, what would be the best way to mimic it?
My goal is to give the background color of the text, and I thought that the <tspan> fill elements would be perfect - they already “outline” the text fragments ( <tspan> ) that represent the lines in multi-line text.
An example I'm working with:
<text x="100" y="100" font-size="30"> <tspan>hello</tspan> <tspan x="100" dy="1.2em">world</tspan> </text>
I tried the "fill" attribute, but it seems to affect the fill (color) of the text, and not the area behind it:
<tspan fill="yellow">hello</tspan>
I also tried setting the background color using CSS:
<style type="text/css">tspan { background-color: yellow }</tspan>
.. but this does not work (at least in Chrome 17 and Firefox 12).
Wrapping tspan in <g> (or text in <g> ) using "fill" also does not work:
<g fill="yellow"><tspan>hello</tspan></g> <tspan><g fill="yellow">hello</g></tspan>
Besides creating a <rect> element located in the same place, something that I would like to avoid, is there another way to achieve this?
css text svg
kangax Dec 30 '11 at 1:12 2011-12-30 01:12
source share