Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use .svg files in a webpage?

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

like image 622
Parastar Avatar asked Jan 06 '10 06:01

Parastar


People also ask

Can you use SVG on web?

SVG is an image format for vector graphics. It literally means Scalable Vector Graphics. Basically, what you work with in Adobe Illustrator. You can use SVG on the web pretty easily, but there is plenty you should know.

How does SVG work in HTML?

SVG is XML based, which means that every element is available within the SVG DOM. You can attach JavaScript event handlers for an element. In SVG, each drawn shape is remembered as an object. If attributes of an SVG object are changed, the browser can automatically re-render the shape.

How do I open a SVG file in my browser?

All modern web browsers support viewing SVG files. That includes Chrome, Edge, Firefox, and Safari. So if you have an SVG and can't open it with anything else, open your favorite browser, select File > Open, then choose the SVG file you'd like to see. It will appear in your browser window.

Is it good to use SVG in HTML?

SVG (Scalable Vector Graphics) files are vector images, meaning they can be infinitely scaled without losing quality. This makes them especially useful for web designers who need to resize without losing sharpness or clarity.


2 Answers

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>
like image 52
Erik Dahlström Avatar answered Sep 29 '22 09:09

Erik Dahlström


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.

like image 34
Caspar Avatar answered Sep 29 '22 07:09

Caspar