Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG as source in <img> tag not displaying in Internet Explorer 11

Tags:

I'm trying to display an SVG file as the src of an <img> tag (which according to caniuse.com/svg-img I should be able to do in all recent browsers). The file is displayed in Chrome and Firefox but Internet Explorer just displays a black box with an x, as shown below:

enter image description here

On opening the file directly in IE, the image is displayed perfectly, so it shouldn't be anything wrong with the actual SVG file. There are no errors or warnings in the IE console, and I'm using the most recent version of the browser.

HTML:

<div id="plot">   <img id="svg" src="plot1.svg" height="100%" width="100%"/> </div> 

Stylesheet:

#plot {     float: right;     width: 650px;     height: 550px;     background: #ffffff; }  #plot svg {     margin-left: auto;     margin-right: auto;     overflow: visible; } 

What are some possible reasons for the appearance of the x?

Edit: The SVG is a large auto generated file, an example of which can be seen here. The code that's being used to generate it is old, could it be something to do with the DOCTYPE at the top? I tried changing it but couldn't get anything to display still.

Edit 2: I have got it working... sort of. When I press F12 and go to the "emulation" tab, for some reason it shows that IE is displaying the page in Document Mode 7, ie. using compatibility mode for IE7, which doesn't support SVG. I can manually click any of the higher versions and it displays fine. My next question is: why? why is it running displaying the page in mode 7, and how do I stop this behaviour?

like image 997
Matt Harris Avatar asked Dec 17 '13 15:12

Matt Harris


People also ask

Does IE 11 support SVG?

SVG does not render in IE11 when used in Lightning components. This is working as designed, and looks to be a limitation of Internet Explorer (IE). More specifically, it is due to using svg.

Can I use SVG in IMG tag?

The quick way: img elementTo embed an SVG via an <img> element, you just need to reference it in the src attribute as you'd expect. You will need a height or a width attribute (or both if your SVG has no inherent aspect ratio). If you have not already done so, please read Images in HTML.

Why is SVG not showing up?

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.

Does SVG work in Internet Explorer?

SVG (Scalable Vector Graphics) is officially supported by all main web browsers, including Internet Explorer. The support spans into a wide variety of image editor software, particularly Inkscape, which uses SVG as its native format (If you want a refresher on SVG, click here).


2 Answers

I've found the problem. I was viewing the page over intranet, and I'm not sure why, but IE's default setting is to "display intranet sites in Compatibilty View". Just had to untick that box in compatibility view settings. SVG doesn't work at all in IE7, so that's why I was getting no image.

like image 183
Matt Harris Avatar answered Oct 20 '22 00:10

Matt Harris


I have found that having a style of "width" on the img (to scale it down) works in Edge and Chrome, but makes it disappear in IE11. Rather setting both "max-width" and "max-height" seems to work in all three of those browsers.

like image 30
bradw2k Avatar answered Oct 20 '22 00:10

bradw2k