I want to display some text inside SVG rect
. Is it possible?
I tried
<svg xmlns="http://www.w3.org/2000/svg">
<g>
<rect x="0" y="0" width="100" height="100" fill="red">
<text x="0" y="10" font-family="Verdana" font-size="55" fill="blue"> Hello </text>
</rect>
</g>
</svg>
But it does not work.
You can use foreignobject for more control and placing rich HTML content over rect or circle
<svg width="250" height="250" xmlns="http://www.w3.org/2000/svg">_x000D_
<rect x="0" y="0" width="250" height="250" fill="aquamarine" />_x000D_
<foreignobject x="0" y="0" width="250" height="250">_x000D_
<body xmlns="http://www.w3.org/1999/xhtml">_x000D_
<div>Here is a long text that runs more than one line and works as a paragraph</div>_x000D_
<br />_x000D_
<div>This is <u>UNDER LINE</u> one</div>_x000D_
<br />_x000D_
<div>This is <b>BOLD</b> one</div>_x000D_
<br />_x000D_
<div>This is <i>Italic</i> one</div>_x000D_
</body>_x000D_
</foreignobject>_x000D_
</svg>
_x000D_
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<g>
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(145,200,103);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(132,168,86);stop-opacity:1" />
</linearGradient>
</defs>
<rect width="220" height="30" class="GradientBorder" fill="url(#grad1)" />
<text x="60" y="20" font-family="Calibri" font-size="20" fill="white" >My Code , Your Achivement....... </text>
</g>
</svg>
Programmatically using D3:
body = d3.select('body')
svg = body.append('svg').attr('height', 600).attr('width', 200)
rect = svg.append('rect').transition().duration(500).attr('width', 150)
.attr('height', 100)
.attr('x', 40)
.attr('y', 100)
.style('fill', 'white')
.attr('stroke', 'black')
text = svg.append('text').text('This is some information about whatever')
.attr('x', 50)
.attr('y', 150)
.attr('fill', 'black')
Programmatically display text over rect using basic Javascript
var svg = document.getElementsByTagNameNS('http://www.w3.org/2000/svg', 'svg')[0];_x000D_
_x000D_
var text = document.createElementNS('http://www.w3.org/2000/svg', 'text');_x000D_
text.setAttribute('x', 20);_x000D_
text.setAttribute('y', 50);_x000D_
text.setAttribute('width', 500);_x000D_
text.style.fill = 'red';_x000D_
text.style.fontFamily = 'Verdana';_x000D_
text.style.fontSize = '35';_x000D_
text.innerHTML = "Some text line";_x000D_
_x000D_
svg.appendChild(text);_x000D_
_x000D_
var text2 = document.createElementNS('http://www.w3.org/2000/svg', 'text');_x000D_
text2.setAttribute('x', 20);_x000D_
text2.setAttribute('y', 100);_x000D_
text2.setAttribute('width', 500);_x000D_
text2.style.fill = 'green';_x000D_
text2.style.fontFamily = 'Calibri';_x000D_
text2.style.fontSize = '35';_x000D_
text2.style.fontStyle = 'italic';_x000D_
text2.innerHTML = "Some italic line";_x000D_
_x000D_
_x000D_
svg.appendChild(text2);_x000D_
_x000D_
var text3 = document.createElementNS('http://www.w3.org/2000/svg', 'text');_x000D_
text3.setAttribute('x', 20);_x000D_
text3.setAttribute('y', 150);_x000D_
text3.setAttribute('width', 500);_x000D_
text3.style.fill = 'green';_x000D_
text3.style.fontFamily = 'Calibri';_x000D_
text3.style.fontSize = '35';_x000D_
text3.style.fontWeight = 700;_x000D_
text3.innerHTML = "Some bold line";_x000D_
_x000D_
_x000D_
svg.appendChild(text3);
_x000D_
<svg width="510" height="250" xmlns="http://www.w3.org/2000/svg">_x000D_
<rect x="0" y="0" width="510" height="250" fill="aquamarine" />_x000D_
</svg>
_x000D_
Source: Stackoverflow.com