Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed SVG within foreignObject?

Tags:

xml

svg

MDN says:

Any SVG elements within a foreignObject will not be drawn, except in the situation where a properly defined SVG subdocument with a proper xmlns attribute specification is embedded recursively.

I've tried setting the proper namespaces to all subsequent elements without any succes.

What i'm trying to accomplish is roughly this:

<svg>
   <foreignObject>
      some html text
      <svg width="10" height="10"><rect fill="red" width="10" height="10" /></svg>
   </foreignObject>
</svg>
like image 781
Emil Avatar asked Sep 12 '25 18:09

Emil


1 Answers

You must have a single element inside the foreignObject, although that element may have children. So you can do this...

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <foreignObject width="100%" height="100%">
    <div xmlns="http://www.w3.org/1999/xhtml">some html text
        <svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><rect fill="red" width="10" height="10" /></svg>
    </div>
  </foreignObject>
</svg>
like image 99
Robert Longson Avatar answered Sep 14 '25 10:09

Robert Longson