Should I use <img>
, <object>
, or <embed>
for loading SVG files into a page in a way similar to loading a jpg
, gif
or png
?
What is the code for each to ensure it works as well as possible? (I'm seeing references to including the mimetype or pointing to fallback SVG renderers in my research and not seeing a good state of the art reference).
Assume I am checking for SVG support with Modernizr and falling back (probably doing a replacement with a plain <img>
tag)for non SVG-capable browsers.
I can recommend the SVG Primer (published by the W3C), which covers this topic: http://www.w3.org/Graphics/SVG/IG/resources/svgprimer.html#SVG_in_HTML
If you use
<object>
then you get raster fallback for free*:*) Well, not quite for free, because some browsers download both resources, see Larry's suggestion below for how to get around that.
2014 update:
If you want a non-interactive svg, use
<img>
with script fallbacks to png version (for older IE and android < 3). One clean and simple way to do that:<img src="your.svg" onerror="this.src='your.png'">
.This will behave much like a GIF image, and if your browser supports declarative animations (SMIL) then those will play.
If you want an interactive svg, use either
<iframe>
or<object>
.If you need to provide older browsers the ability to use an svg plugin, then use
<embed>
.For svg in css
background-image
and similar properties, modernizr is one choice for switching to fallback images, another is depending on multiple backgrounds to do it automatically:Note: the multiple backgrounds strategy doesn't work on Android 2.3 because it supports multiple backgrounds but not svg.
An additional good read is this blogpost on svg fallbacks.
From IE9 and above you can use SVG in a ordinary IMG tag..
https://caniuse.com/svg-img
<object>
and<embed>
have an interesting property: they make it possible to obtain a reference to SVG document from outer document (taking same-origin policy into account). The reference can then be used to animate the SVG, change its stylesheets, etc.Given
You can then do things like
I would personally use an
<svg>
tag because if you do you have full control over it. If you do use it in<img>
you don't get to control the innards of the SVG with CSS etc.another thing is browser support.
Just open your
svg
file and paste it straight into the template.then in your css you can simply eg:
Some resource: SVG tips
Found one solution with pure CSS and without double image downloading. It is not beautiful as I want, but it works.
The idea is to insert special SVG with fallback style.
More details and testing process you can find in my blog.
If you use <img> tags, then webkit based browsers won't display embedded bitmapped images.
If you use inline SVG's, then Explorer won't resize the SVG according to your CSS.Explorer will resize the SVG correctly, but you must specify both the height and width.
I have found that the <object> tag is the only one that works across all browsers. I had to change the width and height (inside the SVG) to 100% in order to get it to resize correctly.You can add onclick, onmouseover, etc. inside the svg, to any shape in the SVG: onmouseover="top.myfunction(evt);"
You can also use web fonts in the SVG by including them in your regular style sheet.
Note: if you are exporting SVG's from Illustrator, the web font names will be wrong. You can correct this in your CSS and avoid messing around in the SVG. For example, Illustrator gives the wrong name to Arial, and you can fix it like this:
All this works on any browser released in the last two years.
Results at ozake.com (in French). The whole site is made of SVG's except for the contact form.
Warning: Web fonts are not precisely resized, and if you have lots of transitions from plain text to bold or italic, there may be a small amount of extra or missing space at the transition points. See my answer at this question for more information.