Use SVG in Websites Now – a How-to Guide


use svg in websites

Summary: SVG graphics is now supported on the majority of web browsers (including mobile devices). Below is a record of the experience of using SVG graphics in my website and why I suggest you to use SVG in your next web project.

In the revamp of my website www.edward-designer.com, it was decided that SVG should be used whenever possible as the technology is maturing. Or is it? I did encounter some problems en route but the result is quite satisfactory.

For the SVG files, I either created them in A.I. and exported as SVG or download free from iconmonstr which offers a searchable set of SVG icons (all in black color). When you try to create text graphics in A.I., remember to outline the texts on exporting, otherwise something weird will show up in Android browsers. Remember to optimize the SVG with an online optimizer if you export it from A.I.

Article Highlights

Change SVG color

Assuming you know little about SVG, you can either open the SVG downloaded from iconmonstr in A.I. and change there or open the SVG file in a text editor and add fill=white or any other SVG named color.

Change the color of SVG icons

Change the color of SVG icons

Actually you can control the appearance of the SVG graphics using javascript. As seen from the above code, the path has an id of image-file-2-icon. Suppose you want to change its fill color to black, you may simply need to include the following javascript code (with jQuery):

$('#image-file-2-icon').attr('fill','black');

Use of SVG in HTML

You can use svg as normal image in the img tag or object tag and control its width and height using css:

<img src="image.svg">

Alternatively, you can add SVG inline by pasting the svg code directly in the html. This has the added benefit of being able to control the style of the SVG via CSS.

SVG can also be used as background images:

<div style='background-image: url(background.svg);'>

Fallback for Old Browser (i.e. IE8)

You may refer to the current state of svg support here. If you use svg in img tag, use the inline method:

<img src="image.svg" onerror="this.src=image.png">

Or, the non-intrusive method:

if (!Modernizr.svg) { $("img[src$='.svg']") .attr("src", $this.src.replace("svg","png")); }

Other Issues to Watch Out For

  1. svg is quite slow in iOS, use simple svg or limit the use to 1 or 2 svgs on each page
  2. types used in svg may not render correctly, outline the text before exporting to svg

 

Support website running for FREE, thanks!

If you find this post helpful and if you are thinking of buying from Amazon, please support the running cost of this website at no extra cost to you by searching and buying through the search box below. Thank you very much for your help!

Edward Chung

Edward Chung aspires to become a full-stack web developer and project manager. In the quest to become a more competent professional, Edward studied for and passed the PMP Certification, ITIL v3 Foundation Certification, PMI-ACP Certification and Zend PHP Certification. Edward shares his certification experience and resources here in the hope of helping others who are pursuing these certification exams to achieve exam success.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

1 Response

  1. Sarah says:

    Thanks for the useful article. http://www.svgminify.com/ would be worth mentioning, it’s a free online tool to reduce the size of your SVG file by removing the superfluous informations that Illustrator or Inkscape embed into a SVG export.