69

I want to know how can one actually use a .svg file In a web page?

bkaid
  • 51,465
  • 22
  • 112
  • 128
Parastar
  • 727
  • 2
  • 7
  • 5

7 Answers7

61

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>
Erik Dahlström
  • 59,452
  • 12
  • 120
  • 139
  • 7
    My 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. – chrisweb Sep 23 '13 at 21:02
  • 2
    @chrisweb That works with `` too. See "Using SVG as an " at http://css-tricks.com/using-svg/. – Brian McCutchon Jul 05 '14 at 19:10
  • @chrisweb How does that work with a background image? – Ken Sharp Feb 27 '16 at 15:50
  • Keep 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. – Hafenkranich May 16 '17 at 16:58
  • does any of these methods support changing of svg icon fill color property also? – Arshad Jun 29 '18 at 13:04
  • @Erik Dahlström, how to display svg image correctly when we have uploaded our svg file to server and then using . – Mahi Nov 05 '18 at 12:34
  • @Ahmadmnzr verify that the server transmits the svg with the correct svg media type, 'image/svg+xml' – Erik Dahlström Nov 09 '18 at 12:18
25

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.

Caspar
  • 1,156
  • 10
  • 7
8

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:

chrisweb
  • 1,428
  • 19
  • 25
  • Do 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? – Snowman Apr 07 '14 at 15:23
  • nope sorry never heard of such a plugin, but maybe it exists – chrisweb Apr 19 '14 at 17:28
6

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/" /> 
Brian McKenna
  • 45,528
  • 6
  • 61
  • 60
  • would this actually show the svg image on any browser? Thanks? – Parastar Jan 06 '10 at 06:35
  • 1
    Thanks, but whats the 'pluginspage' all about? can't catchup with that one? – Parastar Jan 06 '10 at 07:03
  • pluginspage tells the browser where to get a plugin if it can't display SVG files natively (e.g. Internet Explorer). – Brian McKenna Jan 06 '10 at 07:18
  • so the image would not be visiable If the user doesn't download the plugin? – Parastar Jan 06 '10 at 07:29
  • It 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. – Brian McKenna Jan 06 '10 at 08:26
  • 2
    Note 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. – Ken Feb 07 '10 at 05:57
4

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);
}`
Jerome
  • 5,583
  • 3
  • 33
  • 76
3

Raphaël—JavaScript Library. Nice javascript library that is using svg, and gives you a large range of effects!

Also supports most browsers, including IE

code-zoop
  • 7,312
  • 8
  • 47
  • 56
2

I'd like to agree with the answer from "code-zoop". Although this technically doesn't answer your question, it might also be a solution: enter the relevant data straight into the HTML. Either directly as an svg element, or by using Raphaël-JS.

From w3c-schools:

SVG is all suported in In Firefox, Internet Explorer 9, Google Chrome, Opera, and Safari you can

<html>
<body>

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <circle cx="100" cy="50" r="40" stroke="black"
  stroke-width="2" fill="red"/>
</svg>

</body>
</html>

(end of quote)

And to think even more outside the box, depending on how you want to use it, you can also put your 1-color graphics in a webfont. (see for example iconmoon.io )

Ideogram
  • 1,265
  • 12
  • 21