Background color of text in SVG

svg text/javascript
svg background-color
svg text color
svg rectangle with text
svg path background color
svg text background rect
svg text background color d3
svg text width

I want to color the background of svg text similar to background-color in css

I was only able to find documentation on fill, which colors the text itself

Is it even possible?


No this is not possible, SVG elements do not have background-... presentation attributes.

To simulate this effect you could draw a rectangle behind the text attribute with fill="green" or something similar (filters). Using JavaScript you could do the following:

var ctx = document.getElementById("the-svg"),
textElm = ctx.getElementById("the-text"),
SVGRect = textElm.getBBox();

var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
    rect.setAttribute("x", SVGRect.x);
    rect.setAttribute("y", SVGRect.y);
    rect.setAttribute("width", SVGRect.width);
    rect.setAttribute("height", SVGRect.height);
    rect.setAttribute("fill", "yellow");
    ctx.insertBefore(rect, textElm);

How to set the SVG background color ?, How to wrap the text around an image using HTML and CSS ? How to set the SVG background color ? There are two types of images  How to set the SVG background color ? Raster: The images where each pixels represents the individual color within the image. So when we zoom in, the pixels start enlarging Vector: These are the images where the information about drawing has been stored. So when they are zoomed, the image is


You could use a filter to generate the background.

<svg width="100%" height="100%">
  <defs>
    <filter x="0" y="0" width="1" height="1" id="solid">
      <feFlood flood-color="yellow"/>
      <feComposite in="SourceGraphic" operator="xor" />
    </filter>
  </defs>
<text filter="url(#solid)" x="20" y="50" font-size="50">solid background</text>
</svg>

SVG text element, yourself, and break it into multiple lines. You can get some help from the relative positioning possible with the <tspan> element. The SVG <text> element draws a graphics element consisting of text. It's possible to apply a gradient, pattern, clipping path, mask, or filter to <text>, like any other SVG graphics element. If text is included in SVG not inside of a <text> element, it is not rendered.


No, you can not add background color to SVG elements. You can do it programmatically with d3.

var text = d3.select("text");
var bbox = text.node().getBBox();
var padding = 2;
var rect = self.svg.insert("rect", "text")
    .attr("x", bbox.x - padding)
    .attr("y", bbox.y - padding)
    .attr("width", bbox.width + (padding*2))
    .attr("height", bbox.height + (padding*2))
    .style("fill", "red");

How to change the color of an svg element?, Browsers already support background color on SVG text spans for selection highlighting, at least with default styles. Chrome supports it (sort-of,  Specifying Colours in SVG. Colour. Up until here you've had to just trust that "blue" means the colour blue. Now we'll get a little more detailed as to what palette is available and how colours can be specified for programmatic control ("salmon", "orange" and "tomato" don't fit very well in a for/next loop).


The solution I have used is:

<svg>
  <line x1="100" y1="100" x2="500" y2="100" style="stroke:black; stroke-width: 2"/>    
  <text x="150" y="105" style="stroke:white; stroke-width:0.6em">Hello World!</text>
  <text x="150" y="105" style="fill:black">Hello World!</text>  
</svg>

A duplicate text item is being placed, with stroke and stroke-width attributes. The stroke should match the background colour, and the stroke-width should be just big enough to create a "splodge" on which to write the actual text.

A bit of a hack and there are potential issues, but works for me!

Support background-color on SVG text · Issue #622 · w3c/svgwg , Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  SVG background images. SVG can be set as a background image just like PNG, JPG, or whatever other graphics format. At this point, you’ve sort of given up on being able to change the fill. One possibility, which I’d argue isn’t a particularly good one, is to have two versions of every icon, in the respective colors, and swap between them:


Answer by Robert Longson (@RobertLongson) with modifications:

<svg width="100%" height="100%">
  <defs>
    <filter x="0" y="0" width="1" height="1" id="solid">
      <feFlood flood-color="yellow"/>
      <feComposite in="SourceGraphic" operator="xor"/>
    </filter>
  </defs>
  <text filter="url(#solid)" x="20" y="50" font-size="50"> solid background </text>
  <text x="20" y="50" font-size="50">solid background</text>
</svg>

and we have no bluring and no heavy "getBBox" :) Padding is provided by white spaces in text-element with filter. It's worked for me

SVG Text, The SVG text element draws a graphics element consisting of text. Note that the color of the text is set with the * * fill property, the color  Filters will not work in Internet Explorer, so please send a visible image to all of your users (i.e. don’t use a white SVG with an applied filter on a white background, because your IE users will not see anything). Also, remember to use alternative text for icon accessibility, and you’ll be golden to use this technique in your own applications!


<text>, The webkit-specific CSS properties webkit-background-clip and webkit-text-fill-​color can be used to add a fill pattern to a text string and animate  The <text> element can be arranged in any number of sub-groups with the <tspan> element. Each <tspan> element can contain different formatting and position. Text on several lines (with the <tspan> element):


Portable, animated text background fill using CSS and SVG , Notice how the fill color is gray. Here is the SVG code used to generate this image: <text x="20" y="40" style="fill:  SVG has a lot of different elements. The most used ones are. text: creates a text element. circle: creates a circle. rect: creates a rectangle. line: creates a line. path: create a path between two points. textPath: create a path between two points, and a linked text element.


Styling SVG with CSS · WebPlatform Docs, This article covers the basics of styling SVG content with CSS. Make a new SVG document as a plain text file, doc8.svg . SVG demonstration ***/ /* page */ svg { background-color: beige; } #heading { font-size: 24px; font-weight: bold; }  Maketext.io is the modern cool text generator that empowers SVG filters and 800+ open-font-licensed web fonts. While there are already numerous similar websites around, we handcrafted maketext.io with following guideline in mind: Make It Quick, Make It Simple. On the Web, on the Fly. Easily Customizable. Vector Based with Raster Support.