How to use .svg files in a webpage?
Solution 1
See svgweb quickstart and the svgweb project homepage for something that works in all browsers including IE (requires flash plugin).
There are many ways to include an existing svg file:
<img src="your.svg"/>
<object data="your.svg"/>
<iframe src="your.svg"/>
<embed src="your.svg"/>
<div style="background:url(your.svg)">...</div>
Solution 2
If all you want to do is to place an SVG image such as a logo or static diagram, you just need to be careful to provide a fallback for older versions of Internet Explorer (i.e. versions 8 and earlier).
The best and simplest method I've found is to use a .png or .jpg for your fallback, placed using a normal img tag. You then wrap the img tag in an object tag, using the data attribute to place the SVG.
<object data="/path-to/your-svg-image.svg" type="image/svg+xml">
<img src="/path-to/your-fallback-image.png" />
</object>
The img fallback is only loaded and used if the browser doesn't understand SVG.
Solution 3
I recommend putting the svg inline into your document (html5 technique). Just open your SVG file, copy the SVG tag and everything insideof it and then paste it into your html document.
<html>
<body>
<svg></svg>
</body>
</html>
It has the advantage that this allows you to use css to style it, like changing the fill color or applying filters to it like blur. Another advantage is that you save one http request for fetching the svg file if it is inside of your document.
If you want for example to change its position using css, then you have to put the css inside of a style attribute. Styles that are in an external css file will not get applied in most browser as this is a security restriction. For example:
<svg id="mySVG" style="position: absolute; top: 200px; left: 200px;"></svg>
This technique is supported by all browsers except IE8 and below as well as the android 2.3 browser and below.
Read the chapter inline SVG for further details:
If you dont want to put it inline in your page then the best alternative seems to be the object tag and avoid using the embed tag.
Read this for further details about object vs embed vs img tag:
Solution 4
http://www.w3schools.com/svg/svg_inhtml.asp
The best example:
<embed src="rect.svg" width="300" height="100"
type="image/svg+xml"
pluginspage="http://www.adobe.com/svg/viewer/install/" />
Solution 5
Caspar's approach is the proper one. However, I would move the fallback to the CSS, since you probably want to apply some styles to the svg file itself...
<object data="/path-to/your-svg-image.svg" type="image/svg+xml" class="logo"> </object>
CSS
.no-svg .logo {
width: 99px;
height: 99px;
background-image: url(/path-to/your-png-image.png);
}`
Parastar
Updated on September 23, 2021Comments
-
Parastar over 2 years
I want to know how can one actually use a .svg file In a web page?
-
Parastar over 14 yearswould this actually show the svg image on any browser? Thanks?
-
Parastar over 14 yearsThanks, but whats the 'pluginspage' all about? can't catchup with that one?
-
Bogdan Moisa over 14 yearspluginspage tells the browser where to get a plugin if it can't display SVG files natively (e.g. Internet Explorer).
-
Parastar over 14 yearsso the image would not be visiable If the user doesn't download the plugin?
-
Bogdan Moisa over 14 yearsIt would be visible to Firefox, Chrome and Safari users but Internet Explorer users will need to have to plugin installed. If that's a problem then code-zoop's solution does some fancy work to remove the plugin dependency.
-
Ken over 14 yearsNote that code-zoop's solution (RaphaelJS) "removes the plugin dependency" by not actually using SVG at all on IE. If you're looking specifically to display an "svg file", that is probably not going to help you. For SVG + all major browsers, IE users will need a plugin.
-
chrisweb over 10 yearsMy favorite is not in this list, just open your svg in a text editor and put its content inline into your html document, this will allow you to apply filters to it and style the svg image with css.
-
Jerome over 10 yearsalternatively move the fallback into the CSS itself
-
Darryl Snow about 10 yearsDo you know of a way to automate this? Rather than copying from the SVG file and pasting into your HTML, is there a grunt plugin or something that can inject it into your page?
-
chrisweb about 10 yearsnope sorry never heard of such a plugin, but maybe it exists
-
Brian McCutchon almost 10 years@chrisweb That works with
<object>
too. See "Using SVG as an <object>" at css-tricks.com/using-svg. -
Ken Sharp about 8 years@chrisweb How does that work with a background image?
-
Hafenkranich almost 7 yearsKeep in mind that if you don't include it directly in your content document (like crisweb suggests) you might have something similar to FOUC with your svg making it look like it gets reloaded every time you switch a page. Important for header logos and the like.
-
Junaid Atari about 6 yearsthat JS library url is broken btw
-
code-zoop about 6 yearsI added the answer 8 years ago. But Hey, here you have the link to the github repo github.com/DmitryBaranovskiy/raphael
-
Arshad almost 6 yearsdoes any of these methods support changing of svg icon fill color property also?
-
Mahi over 5 years@Erik Dahlström, how to display svg image correctly when we have uploaded our svg file to server and then using <img src="your.svg"/>.
-
Erik Dahlström over 5 years@Ahmadmnzr verify that the server transmits the svg with the correct svg media type, 'image/svg+xml'