My Playground

SVG for Web Design

Asif Nawaz

Writen on August 15, 2017

Category: web design, web development

What is SVG

SVG stands for Scaleable Vector Graphics. If you work with graphics or have some idea about it you know what’s the advantage of working with vector graphics. We know about some vector format like eps, ai etc. svg is yet another format specially built for web to make two dimensional graphics.. The SVG specification is an open standard developed by the World Wide Web Consortium(W3C) since 1999. SVG is written by XML language which is pretty semantic but it’s very hard to write with hand though. All modern browsers support SVG. Checkout full support details here.

Why SVG?

SVG is scaleable and programmable like if you want to use a small SVG file and need to make it huge size it won’t loose any quality as it’s a vector. Let me tell you a scenario, since retina display come to market we front end developers use another high resolution size of image for this this display which is costly and this 2x size of images takes huge space as well. If we need to keep duplicate image for every image. This is so costly. If we use SVG for we don’t need to do anything a single SCV perform well in any display size.

This is a basic use of SVG, now think of a complex shape which is not possible to build with HTML and css, now what we’d do? what if you need to animate the image parts and perhaps need to change it depending on something like a chart do. This is where SVG is rocking as it is programmable. You can change any SVG and transform it to another shape easily with JavaScript and it support CSS for styling which is pretty similar to normal CSS.

Basic understanding of SVG tags

SVG declaration is similar to HTML, lets see a basic SVG file content

SVG code is readable like this one. if you know basic HTML then It’s easy to understand. First tag is svg tag which enclosed all other things. There are height and width attributes which declare the size of this SVG image. Inside SVG tag there is a circle tag which means it will draw a circle and it has some attribute to declare its size, style, position. If you look at the attribute you’ll see they are pretty semantic as well. let me explain.

cx & cy: Those attributes hold coordinates value for circle center. by default they are 0. if you draw a circle with 50px radius then the cx and cy value should be at least 50 to make it visible in the screen.

r: This is radius of the circle

stroke: Stroke value is the color name or color code.

stroke-width: This is to set stroke width which is known as border in css.

fill: Fill is the background of the circle. we’re use to with background while writing html. It support Hex color codes, color names and rgb value as well.

 

SVG has many other tags to draw shapes, like <rect/>, <path/>, <polygon/> etc. Actually it’s not possible to write svg everyday. SVG rockstar Dmitry Baranovskiy also said he doesn’t recognize everything of SVG. Then what is the solution? the solution is we will use a editor for converting or changing anything it can be Illustrator or anything else. But we should have some basic understanding about the tags and attributes because we’d need that knowledge to program it with JavaScript or change its style with CSS.

What you can do with SVG

You can do many things with SVG, this technology is a bless for a web developer. Let me make a small list

  • Animated 2D graphics
  • Charts
  • SVG supports on the fly modification with JavaScript
  • SVG and its parts support DOM events
  • Raster image can be masked with SVG
  • Any complex shape can be build with SVG
  • SVG support animation
  • SVG icons is pretty useful for web design.
  • SVG can be embed inside HTML with object, iframe, embed or even img tag.
  • SVG can be used as background in CSS

Preparing and Sanitizing SVG for Web Design

Every vector designing software has feature to export in SVG format. In Illustrator there is an option called export. First we need to open or draw something. Now go to File > Export > Export As and then select the format svg from dropdown and click on Export button. A dialog box will appear like below.

 

 

This is a special for optimizing SVG for web use. For using in development you can save from normal file saving feature by selecting svg format. Please remember while exporting svg with text it’s better to make the text outline to avoid font issue. You will see the option Create Outline by clicking on a text object.

Using SVG in Webpages.

There are few different tags to embed svg in webpages like embed, object and img.

<img/> this is pretty same like normal image. Using img tag it won’t be possible to access parts of the SVG with JS or CSS so it’s avoidable while we need to access.

For fallback option you can use the oberror event, it would load the png file while the svg is not supported.

With Object and Embed tag it’s also easy like img tag and this way it’s possible to access the SVG inner elements with JS or CSS. Object tag supports fallback content if the browser doesn’t support SVG like you can keep a raster image embed or without embedding fallback is okay. If you want to give me interaction to your svg then object is a good way.

Embed tag is good for old browsers support.

If you want to go deep about SVG Fallback try this link.

SVG as CSS Background

In CSS its possible to use svg as background of any html element just like a png or jpg like below,

If the svg fail to load the the png will be used as fallback image.

 

This is another way to use embedded SVG. you can save server request by using this way also if you want to use a different background on hover it’s a pretty good way.

Now the last thing, you can copy the content from a svg file and paste it to HTML document directly. SVG codes is like below in svg file, you can just take the <svg> tag. <?xml> tag is not required.

Tools and Libraries!

  • Online SVG Optimizer
  • Convert SVG image to icon library with IcoMoon
  • SVG To React is pretty handy CLI to make a svg to react component. This is life saving tool for React JS developers.
  • SVGO is a good SVG Optimization CLI Tool for web use.
  • Snap SVG for animation and manipulation

Good Reads

Speech

 

Would you use SVG from now on? let me know how much you like this post in the comment box. good bye for today 🙂