132

I have an SVG document, and I would like to include an external svg image within it, i.e. something like:

<object data="logo.svgz" width="100" height="100" x="200" y="400"/>

("object" is just an example - the outer document will be SVG rather than xhtml).

Any ideas? Is this even possible? Or is the best thing for me to simply slap the logo.svg xml into my outer SVG document?

8 Answers 8

177

Use the image element and reference your SVG file. For fun, save the following as recursion.svg:

<svg width="100%" height="100%" viewBox="-100 -100 200 200" version="1.1"
     xmlns="http://www.w3.org/2000/svg">
  <circle cx="-50" cy="-50" r="30" style="fill:red" />
  <image x="10" y="20" width="80" height="80" href="recursion.svg" />
</svg>
17
  • 6
    Thanks, for some reason my googling for this just did not work until after I posted this question. I note that width and height must be present for the image to render.
    – Marcin
    Mar 27, 2011 at 18:35
  • 30
    One interesting observation: the latest versions of Firefox, Chrome, and Safari all show only one level of recursion (two dots) using the above. However, if you save the above as "a.svg" and change the image to "b.svg", and then also save it as "b.svg" with the image referencing "a.svg", then Firefox will show additional levels of recursion for each time you reload the alternating files. It appears to cache result each time you load the file, going one level deeper.
    – Phrogz
    Mar 27, 2011 at 20:16
  • 9
    @IanStormTaylor An SVG element does not have style properties itself; rather the items inside the SVG element have style. However, when using <image> in SVG (or <img> or <embed> in HTML) to reference an SVG file you are not given access to the underlying DOM. As such, no, you cannot style elements inside an SVG element that has been referenced by an <image>.
    – Phrogz
    Jan 16, 2012 at 16:43
  • 2
    @proteneer <image xlink:href="data:image/svg+xml;utf8,&lt;svg …&gt;… &lt;/svg&gt;" />. (If you're using JavaScript to set the href attribute, you do not need to escape the < etc. characters.)
    – Phrogz
    Feb 2, 2015 at 4:15
  • 4
    xlink:href is deprecated, now you should just use href. Could you update your answer to include that? Aug 16, 2020 at 8:51
139

Or you can actually embed child svg in parent svg like this:

<svg>
    <g>
        <svg>
            ...
        </svg>
    </g>
</svg>

demo:
http://hitokun-s.github.io/old/demo/path-between-two-svg.html

7
  • @toshi do you have another example of your answer? i am trying but failing to implement your advice. my 'outer' SVG sets a circle and gradients. my inner SVG is an object. stand-alone, the inner SVG works as expected. but the inner SVG does not display in my implementation of your advice. hence, my request to see another example.
    – Jay Gray
    Feb 20, 2019 at 15:10
  • 1
    +1 for mentioning a self-contained alternative. How does positioning/sizing of such an embedded svg work?
    – bluenote10
    Aug 27, 2020 at 21:21
  • I would like to add that you can add transformations to this <g> element: translation, rotation, etc. This way you can dynamically reposition elements Nov 13, 2020 at 12:59
  • For instance, to move an element you would write: <g transform="translate(30,10)">...</g> Nov 13, 2020 at 13:00
  • Wait, why is it even legal? May 8, 2022 at 2:40
52

It is worth mentioning that when you embed SVGs into another SVG with:

<image x="10" y="20" width="80" height="80" xlink:href="image.svg" />

then the embedded SVG takes a rectangular shape with given dimensions.

That is to say, if your embedded SVG is a circle or some shape other than a square, then it becomes a square with transparency. Therefore, mouse events get trapped into that embeded square and do not reach the parent SVG. Watch out for that.

A better approach is using a pattern. To fill a shape, either a circle, a square or even a path.

<defs>
 <pattern id="pat" x="0" y="0" width="500" height="500" patternUnits="userSpaceOnUse">
   <image x="0" y="0" width="500" height="500" xlink:href="images/mysvg.svg"></image>
 </pattern>
</defs>

Then use the pattern like this:

<circle cx="0" cy="0" r="250" fill="url(#pat)"></circle>

Now your mouse events do not get stuck into transparent image squares!

1
  • That fill pattern is perfect, thank you. For smaller inserts or smaller viewboxes, coders may want to reduce all width and height in equal measure. May 15, 2020 at 21:13
11

I found that using the <image> tag gave a low-quality render of the embedded file. However the following technique worked (to embed an SVG file inside an SVG file - not necessarily for rendering on an HTML page):

  • Edit the SVG file in a text editor.

  • Find the end of the metadata:

    </metadata>
      <g
       id="layer1"
       inkscape:groupmode="layer"
       inkscape:label="Layer 1">
    
  • Insert this line after that group tag:

    <use xlink:href="OTHERFILE.svg#layer1" y="0" x="0" />
    
  • In this case we are including OTHERFILE.svg into the file, and all of layer1 (the first and default layer).

  • Save this and then open the file in Inkscape.

This technique is useful for having a standard background or logo on every page. By putting it first in the file it will be rendered first (and thus at the bottom). You could also lock it by adding this attribute:

sodipodi:insensitive="true" 

In other words:

<use xlink:href="OTHERFILE.svg#layer1" sodipodi:insensitive="true" y="0" x="0" />
1
  • @WilliamEntriken What do you mean by "external files"? The method I described uses an external file, namely the file with the other stuff in it. Apr 10, 2020 at 7:48
10

Note xlink:href has been deprecated, just use href instead, e.g.

<svg viewBox="0 0 512 512">
  <image width="512" height="512" href="external.svg"/>
</svg>

viewBox, width and height values (in this answer) are simply for illustration purpose, adjust the layout accordingly (read more).

Since <image> shares similar spec as <img>, meaning it doesn't support SVG styling, as mentioned in Christiaan's answer. For example, if I have the following css line that set the svg shape color to be the same as the font color,

svg {
  fill: currentColor;
}

The above style wouldn't apply if <image> is used. For that, you need to use <use>, as shown in Nick's answer.

Note id="layer1" and href="OTHERFILE.svg#layer1" values in his answer are mandatory.

Meaning you have to add the id attribute to the external svg file, so you need to host the (modified) external svg file by yourself (your website) or somewhere else. The resulting external svg file looks like this (notice where I put the id):

<svg id="logo" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
  <path d="..."/>
</svg>

The value of id can be anything, I use "logo" in this example.

To embed that svg,

<svg viewBox="0 0 512 512">
  <use href="edited-external.svg#logo"/>
</svg>

If you use the above svg as inline in your html, you don't need xmlns attribute (at least what I know from svgo).

1
  • 2
    viewBox is not mandatory, if you omit it you'll get a different layout, in some cases that might be what you want though. Safari has only just started to support href. Jun 8, 2019 at 12:30
9

I needed to embed a SVG in my SVG but also change its color and apply transforms.

Only Firefox supports the "transform" attribute on the nested svg elements. Changing the color of <image> is also not possible. So a combination of both was needed.

What I ended up doing was the following

<svg>
  <image x="0" y="0" xlink:href="data:image/svg+xml;base64,[base64 of nested svg]"></image>
</svg>

This works on at least Firefox, Chrome and Inkscape.

This behaves the same as the child svg in the parent svg answer with the exception that you can now apply transforms on it.

2

How about using the SVG <use> tag to embed the extra SVG?

1
0

Very Easy

<image x="120" y="720" width="1000" height="900" href="assets/img/image.svg" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.