Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to append a div inside an SVG element?

I am trying to append an HTML div inside an SVG, which I am trying to create a graph. I am trying to append it using the code below, but when I inspect element using Firebug, the div is shown inside rect element code, but it doesn't show up in the graph UI.

Is it something wrong with the method that I am trying or is it impossible to append a div inside an SVG?

    marker.append("rect")    .attr("width", "50px")    .attr("height", "50px")    .append("div")    .attr("id", function(d) {       return "canvas_" + d.key.split(" ").join("_");    })    .style("width", "50px").style("height", "50px"); 
like image 682
sam Avatar asked Jul 11 '13 14:07

sam


People also ask

Can SVG elements be nested?

The SVG format allows for the nesting of SVG graphics. It is possible for an “<svg>” elements, to be placed within another “<svg>” element. Though, within a nesting, the absolute placement is limited to the respective parent “<svg>” element.

Can we add div inside a?

Nothing is wrong with placing a div inside a tag. In fact, you can place just about anything inside a tag as long as they are categorized as transparent , except that no descendant may be interactive content (eg: buttons or inputs) or an a element, and no descendant may have a specified tabindex attribute.

Can you put HTML inside SVG?

SVG images can be written directly into the HTML document using the <svg> </svg> tag. To do this, open the SVG image in VS code or your preferred IDE, copy the code, and paste it inside the <body> element in your HTML document.

Is SVG a DOM element?

SVG is a language for describing 2D graphics in XML. Canvas draws 2D graphics, on the fly (with a JavaScript). SVG is XML based, which means that every element is available within the SVG DOM.


2 Answers

You can't append HTML to SVG (technically you can with foreignObject, but it's a rabbit hole). Furthermore, visible elements in SVG can't be nested, so elements such as circle, rect, path and such can't have children elements.

like image 76
methodofaction Avatar answered Oct 01 '22 21:10

methodofaction


I would also consider using <g> element as it serves similar purpose as <div> by grouping objects. More about it here.

like image 26
Karolis Ramanauskas Avatar answered Oct 01 '22 23:10

Karolis Ramanauskas