Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG <image> tag inlined in HTML not loading image in browser

Trying to load an image in SVG using an HTML page:

<html>
...
<svg>
<image x="330" y="137" width="320" height="38" preserveAspectRatio="none" href="/img/title.png"></image>
</svg>
...
</html>

All other elements in the svg load up (paths shapes text) except image. Any way to get this working in browsers? I've tried Chrome, Firefox, IE. All browsers are missing the image.

Using Chrome I found that Chrome doesn't even send the request for the image. I have a hunch it's the same for IE and Firefox.

Any solutions?

like image 867
Kenneth Avatar asked Jul 10 '11 19:07

Kenneth


People also ask

Why are my SVG images not showing?

If you are trying to use SVG like <img src="image. svg"> or as a CSS background-image , and the file is linked to correctly and everything seems right, but the browser isn't displaying it, it might be because your server is serving it with an incorrect content-type.

Why is SVG not showing up in Chrome?

Chrome browser will not display svg image, if it doesn't have with attribute with value in svg source code. Edit your SVG source code and add width attribute with desired value.

How do you inline SVG in HTML?

How to use inline SVG images. 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.

Can you embed SVG in HTML directly into an HTML page?

You can embed SVG elements directly into your HTML pages.


2 Answers

You need to use:

<image ... xlink:href="...">

See the SVG Document Structure spec.

like image 158
RichieHindle Avatar answered Oct 03 '22 00:10

RichieHindle


This is not the case in question, but I ran across a similar issue with an <image href="/path/to/file.svg"> tag not showing up in Edge, while working correctly in Firefox and Chrome.

It turns out that if you don't explicitly set both a height and a width attribute on the <image> tag Edge won't show the image. This is not the case in Chrome and Firefox where width alone was enough.

like image 38
Tommaso Amici Avatar answered Oct 03 '22 00:10

Tommaso Amici