SVG as an Icon System

SVG is particularly good for icons. Besides the resolution independence and styling possibilities, the shapes for icons are generally simple and thus the files size is tiny and can be combined into a single web request like a sprite or icon font.

SVG Patterns

In SVG you can fill shapes with colors and gradients, but you can also specify a <pattern> to fill with. <pattern>s are groups of other SVG elements that repeat, so it’s kinda like putting some <svg> in your <svg>.

About viewport, viewBox, and preserveAspectRatio

The browser window you are looking at right now is a viewport. If there was an <iframe> on the page, that would have its own viewport. So too does <svg>! It’s a little tricky though, because SVG also has a viewBox which is the actual space in which the SVG elements are drawn. That, combined with preserveAspectRatio, determine how and where the SVG are drawn within that viewport.

SVG Gradients

Whereas a fill in SVG is just an attribute (or applied in CSS), SVG gradients are literally a special element like <linearGradient>. CSS has gradients as well, but because SVG gradients (can be) in the DOM, there might be cases where they are easier to manipulate.

SVG and Fragment Identifiers

The <use> element in SVG allows you to copy a bit of SVG for use again elsewhere. But that only works for inline SVG. If you wanted to use just particular bits inside an SVG file in CSS (background-image) or HTML (<img>), you’ll need fragment identifiers.

SVG and Text

SVG has a <text> element which allows you to add (you guessed it) text to a graphic. Real, native web text, not outlines of characters, which 1) makes it more useful (selectable, searchable) 2) makes it more accessible 3) keeps the size down. Just like regular web text, the fonts you have available are limited to what is available on a given browser/platform/version or what is loaded via @font-face.

Animating SVG

There are a variety of ways to animate SVG. SVG syntax has an tag specifically for it, although it’s a touch complicated. That’s often referred to as SMIL (Synchronized Multimedia Integration Language). If you’re using inline SVG, you can also target the SVG (or shapes within) with CSS and animate using CSS animations. You can also use JavaScript to manipulate (and thus animate) SVG.

Organizing SVG

Much like there are HTML tags to organize content, there are SVG tags to organize graphics. For instance, The <g> (group) tag is analogous to <div>. It doesn’t have much meaning all by itself, but it’s useful because it means you can style it and let those styles affect the entire group.

SVG Build Tools

A common way to work with SVG on a site is to have a “folder full of SVGs” that you intend to use throughout the site. For performance reasons, you wouldn’t want to request each one individually. These build tools help combine them together in one way or another.

IcoMoon by Keyamoon (select icons you want to use, output just that as SVG sprite. Drag and drop on your own SVG. Save project to update later.)

Getting SVG to Use

Download vector stuff from the internet. Remember anything vector can be turned into SVG (e.g. you might find have an .eps or .pdf, but it has vector stuff in it, so open/manipulate in Illustator, Save As .svg).

Stock photo sites typically have a way to return only vector search results. (e.g. Shutterstock or Vecteezy)

Remember fonts are vector, thus icon fonts are vector, thus any icon in an icon font set can be SVG. (Download and activate font, type character in Illustrator document in that font, convert to outlines, Save As .svg) Here’s a big list of icons fonts by me.

SVG Demos

SVG Filters

There are CSS filters, which allow for familiar things like blur. SVG has its own set of filters (which is where the CSS ones came from) that include those familiar things, but also much more, including weird and unique things more reminiscent of Photoshop filters. SVG filters can be applied to HTML content as well as SVG content.

Data URIs and SVG

One of the advantages of inline SVG can be the fact that no web request needs to be made. The information to draw that SVG is right there. Data URI’s (sometimes “Base 64″ encodings, sometimes actual <svg> syntax) also do no-request rendering (that’s the whole point, really.)

Optimizing SVG

It’s very smart to optimize images before use on the web. For raster images, even after you’ve saved at the smallest, lowest quality you are comfortable with, tools like ImageOptim can trim off even more file size without further affecting quality.

Talks as Videos

Podcasts

SVG and JavaScript

One of the advantages of SVG is that the shapes are in the DOM. For instance, perhaps a <svg> has a <rect> and a <circle>. Through JavaScript, you could bind an event handler to just the <rect> and a different one to just the <circle>.

SVG Browser Support

SVG is supported in all the current versions of all browsers and several versions back. The biggest concern is generally IE 8 and down, and Android 2.3 and down. But that’s just basic SVG support (inline, <img>, CSS background). There are sub-features of SVG that have different levels of browser support. Can I Use… does a good job of tracking that stuff.

SVG Fallbacks

If you need to support browsers old enough to not support SVG (see the Browser Support section), there are plenty of ways to handle fallbacks to resources that do work (e.g. replacing an image.svg with an image.png or the like).

SVG for Everybody by Jon Neal. Syntax: <svg><use xlink:href="spritemap.svg#icon"></use></svg>. Either just works, or Ajaxs and replaces the (for IE), or puts in fallback PNG.

SVG injector from the Iconic folks (a really nice icon set as inline SVG and every kind of fallback). Syntax: <img data-src="svg/thumb-up.svg" data-fallback="png/thumb-up-green.png">. Will inject inline SVG or a fallback.

SVG Specs

SVG Tiny 1.2 (Not really used as far as I know, even though you can export as it from Illustrator. It was intended for stuff like old Blackberry’s)

SVG 1.2 Tiny is a profile of SVG intended for implementation on a range of devices, from cellphones and PDAs to laptop and desktop computers, and thus includes a subset of the features included in SVG 1.1 Full