The advantages of using SVG (Scalable Vector Graphics)
The use of Scalable Vector Graphics (SVG) are among the most important web design trends for 2014. It is recommended by W3C (World Wide Web Consortium) since 2003, but it was underused due to partial support on Internet Explorer (IE). But things are changing today…
In my article I will briefly present the SVG concept and discuss the practical benefits of using SVG in web design.
What is SVG?
SVG stands for Scalable Vector Graphics, and is an XML-based vector image format for 2D graphics which support interactivity and animation. For more than a decade, SVG has been underused, not being fully supported by Internet Explorer.
But things changed since then, and SVG elements are now used more and more, especially on browsers that support HTML5.
Today, the major internet browsers support SVG:
- Internet Explorer 9+
- Firefox 4+
- Chrome 4+
- Safari 4+
- Opera 9.5+
SVGs are declared using eXtensible Markup Language (XML), which uses tags like HTML.
For example, the following code draws a white circle with a black border:
<svg height=”100″ width=”100″>
<circle cx=”100″ cy=”100″ r=”50″ stroke-width=”4″ stroke=”#000″ fill=”#fff” />
- The SVG graphic is declared by using the <svg>…</svg> tags
Height and width attributes of the <svg> element define the width and height of the SVG
- The cx and cy attributes define the x and y coordinates of the center of the circle. If cx and cy are omitted, the circle’s center is set to (0,0)
- The r attribute defines the radius of the circle
- The stroke and stroke-width attributes control how the outline of a shape appears
- The fill attribute refers to the color inside the circle
It is important to know that XML is stricter than HTML, so omitting a closing tag could make the file invalid and the SVG not to be rendered.
The W3C SVG1.1 specification defines 14 main features:
- Basic shapes: straight lines, polygons, circles, ellipses, and rectangles with or without rounded corners.
- Paths: outlined or filled paths containing curved or straight lines.
- Text: on straight or curved paths in any direction.
- Painting: fills and outlines using solid colors, gradients, patterns, transparency, and markers (line terminators such as arrow heads).
- Color: fill and stroke properties defined using standard 3 or 6-digit hex or rgb values.
- Gradients and patterns: CSS3-like gradient declarations or bitmap backgrounds.
- Clipping, masking and compositing: using elements to outline regions which can be painted.
- Filters: effects applied to all elements within a container, e.g. blurring, lighting, color adjustments, etc.
- Linking: hyperlinks to other documents.
- DOM Scripting: accessing and manipulating SVG elements using the Document Object Model.
- Animation: built-in animations using Synchronized Multimedia Integration Language (SMIL).
- Fonts: text glyphs defined in an SVG file which can be used as a standard font.
- Metadata: titles, descriptions, subjects, creators and other properties about the SVG image.
Another way to create SVGs is to draw them using a vector graphics editor, which is preferable for those who don’t know XML, or for those who want to create more complex graphics. We’ll refer to these tools briefly in the following lines.
But now let’s see the main reasons why every web designer should consider using SVGs.
Advantages of using SVG
- Vector graphics are scalable
The greatest advantage of vector graphics is the possibility to scale them to whatever size we want without losing quality. (see the image below) This is mainly the reason why they are ideal for designing company logos and other designs that are most often resized. Every designer should pay attention in assuring a good scalability to their work.
Also, resolution independence of scalable graphics is a really hot topic these days because high resolution devices are more and more often used and delivering a responsive content is desirable.
2. Easy to create.
Creating simple graphics with XML is quite easy. But what if we want to create more complex graphics? There are plenty user friendly vector graphics editors that could be used to create SVG graphics, like Adobe Illustrator, Macromedia Freehand, Corel Draw. There are also some nice free tools like Inkscape, OpenOffice, LibreOffice Draw and svg-edit (online tool).
3. Easy to edit
SVG are easy to edit, which represents a huge advantage over rasterized graphics. If you want to make changes to your vector graphics, all you need is a text editor, or even easier, use a vector graphic editing tool. Important to know, components in a vector graphic can be individually manipulated, so when editing, there’s no need to build anything from scratch. Changing basic appearance attributes like color, outline and fill can be done very easily and quickly.
4. Smaller file sizes
Having a smaller file size makes transferring and loading graphics much faster on the web. That’s the reason why many people prefer to use vector graphics, for you could easily view them on the web without needing to wait for a long time in order to see the entire image. Even if you’ll make a huge image, it will still have a smaller file size compared to a similar raster image.
5. Ideal for details
Since vectors uses lines, it is easier to create very detailed graphics. Also, illustrations will look sharper than high resolution photographs no matter what you’ll use them for. They are a lot clearer and easier to understand. Vector graphics also look better in print and are sharper, too.
6. File size based on complexity
Vector images are small files. They are based on how complex or difficult the image is, how intricate the lines are and how complicated the points are. The size of these graphics is not based on the size or color depth.
7. Accessible DOM Node-Based API
SVGs have a DOM, which opens lots of possibilities to control the graphics’ appearance and behavior. It’s really easy to attach event handlers and manipulate elements like you would for other HTML blocks. That’s why, you can inspect SVG elements in the browser like any other HTML element.
The DOM node-based API of SVG also gives you the possibility to create SVG document-based images on the server-side.
There are multiple JS libraries created to assist you in this world, like D3.js, Raphael, Snap.svg, Processing.js, JSDrawing, PlotKit, SVGWeb and Paper.js.
8. Reduced HTTP Requests
If SVGs are embedded directly into an HTML document with svg tag, the browser does not need to do a request to serve the graphic, like in the case of using images with the <img> tag. This results in better load performance for the website
9. SVGs are SEO friendly
Unlike bitmaps, XML is inherently machine-readable, so static SVG files can be read, parsed and indexed by search engine bots. Google has been indexing SVG content since August 2010 and results can be found in the standard and image search systems.
Replacing a few PNGs with SVGs is unlikely to improve your rankings. However, if you’re displaying data as images without textual fallbacks, switching to SVGs could offer an SEO boost.
And a few disadvantages…
It wouldn’t be fair to omit to speak about some of the downsides of SVG. Fortunately there are few to mention. They are:
1. Complex development – SVG code which is structured XML, can be quite lengthy and complex, difficult to troubleshoot for errors.
2. Performance issues – in case of over-used and complex animations the WebKit engine could be noticeably slower.
3. Not fully supported by older browsers (Internet Explorer 8 and lower versions.)
There are still some good workarounds to extend browser support, like Raphael.js or using the technique of replacing SVGs with static images in older browsers.
Some practical uses of SVG
Let’s look at a few practical applications for SVG.
Because SVG’s number 1 strength is basic vector shapes, it works very well for creating graphs and infographics. Not only is it great for creating static graphs from given numbers, but it is also well suited for “live” graphs, fed dynamically by AJAX requests, user input, or randomly generated data.
- Road Map
Road maps are built of hard lines and exact shapes. These shapes can be represented easily with vector graphics, and lend themselves to zooming into the map for further detail.
- Complex UI elements
Sometimes we may need to create a more complex UI element which consists of a number of shapes, keeping in mind the responsiveness in the mean time. Creating them using HTML and CSS would usually raise many problems. (i.e positioning, masking, layering and styling issues)
A much better solution would be to draw the element in a graphic editor and save it out as an SVG file. This would allow us to have a single, scalable element without worrying about managing multiple divs.
Most logos are vector-based graphics. So why not define an SVG document as your logo, and drop it anywhere, scaling it on the fly to whatever size it needs to be without compromising quality and saving bandwidth in the same time?
- Simple Games
Even if CANVAS is better for game rendering (games that use pixel-based art and animation), SVG could be a great alternative for simple games that require less character animation and more information display(for example Sudoku).
This article was published in Today Software Magazine #27.
- The basics of using SVG : http://www.w3schools.com/svg/svg_intro.asp
- Using SVG, a practical guide: http://css-tricks.com/using-svg/
- SVG workflow: http://danielmall.com/articles/svg-workflow-for-designers/
- A compendium of SVG information: http://css-tricks.com/mega-list-svg-information/
- About animating SVG: http://24ways.org/2013/animating-vectors-with-svg/
- Making responsive SVG :http://demosthenes.info/blog/744/Make-SVG-Responsive
- SVG Vs Canvas: http://www.sitepoint.com/7-reasons-to-consider-svgs-instead-of-canvas/
- SVG and SEO: http://www.rocketmill.co.uk/exploiting-svg-images-for-seo
- Some nice SVG examples: http://www.creativebloq.com/design/examples-svg-7112785