Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG text element with whitespace not preserved in IE

I want to preserve the white space in a single SVG < text> element.

This works fine with xml:space="preserve" attribute in the text element in all but IE browser.

  <text x="0" y="15" fill="red" xml:space="preserve">I     love     SVG!</text>

Here is the jsfiddle, try to open this fiddle in Chrome/Firefox and in latest IE, notice that whitespace in the text element text is not preserved in IE.

jsfiddle

Any workaround so that this works in IE as well?

like image 870
Chetan Avatar asked Dec 16 '14 06:12

Chetan


People also ask

Is SVG text editable?

But most importantly, what's the best way to make SVG text editable? document. getElementById("element"). contentEditable = "true"; You can also use the ref contenteditable="true" in an HTML element like so: <div contenteditable="true"> .

Can SVG contain text?

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.

What is xml space?

The xml:space attribute is an XML-defined attribute that declares the significant white-space processing behavior within an object element. This behavior is relevant for all content (inner text) contained within the element where xml:space is declared, and also scopes to child elements.

What is whitespace CSS?

white-space is a CSS property that helps control how whitespace and line breaks within an element's text are treated. The white-space property can take these values: normal : The default value. Multiple whitespaces are collapsed into one. The text wraps to the next line when needed.


2 Answers

I guess you could replace each space by &#160; (that's the unicode non-breaking space character).

In javascript it would be written as \u00A0 though.

like image 106
Robert Longson Avatar answered Oct 16 '22 01:10

Robert Longson


If you are using d3 this works well:

.attr("style","white-space:pre")
like image 6
David Avatar answered Oct 16 '22 02:10

David