Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background color of tspan element

Tags:

text

css

svg

Is it possible to give SVG <tspan> element background color? If not, what would be the best way to simulate it?

My goal is to give text background color, and I figured that filling <tspan> elements would be perfect — they already "outline" text chunks (<tspan> elements) that represent lines in multiline text.

The 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 "fill" attribute but it seems to affect fill (color) of text, not the area behind it:

<tspan fill="yellow">hello</tspan>

I also tried setting background-color via CSS:

<style type="text/css">tspan { background-color: yellow }</tspan>

..but that doesn't work (in at least Chrome 17 and Firefox 12).

Wrapping tspan in <g> (or text itself in <g>) with "fill" doesn't work either:

<g fill="yellow"><tspan>hello</tspan></g>
<tspan><g fill="yellow">hello</g></tspan>

Aside from creating a <rect> element positioned at the same location — something I'd like to avoid — is there another way to achieve this?

like image 989
kangax Avatar asked Dec 30 '11 01:12

kangax


People also ask

How do I put background color behind text in HTML?

How to Add Background Color in HTML. To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.

Does span have background color?

Example 5: A span tag can be used to set color/background color as a part of a text.

How do I change the background color of text in CSS?

Changing Text Background Color in CSS. To change the background color of the inline text, go to the <head> section. Simply add the appropriate CSS selector and define the color and background-color property with the values you want.


1 Answers

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).

like image 101
Erik Dahlström Avatar answered Sep 29 '22 16:09

Erik Dahlström