Get Some SVG In Your Life

Nov 1, 2016

Scalable Vector Graphics, or SVGs, are two dimensional vector graphics that are created using the xml language. They were created specifically for the web and give web developers the ability to display vector graphics in an editable format.

Before we worry about the details of the attributes and values in the SVG element, I want to explain the theory behind the code above. We create an SVG element with a few attributes. Then we create a circle element within that SVG element. The circle element applies a couple attributes that define where to place the element and how large it should be.

This code will create an SVG that is familiar to a lot of you, Cartman. Wowzers. This code might look complex and intimidating and that’s because it is! But let’s dig into the code a bit. So just like the SVG before, we set an SVG element with a couple attributes. Within that SVG element we have a bunch of other elements. Each one of those elements creates a shape that make up Cartman. The intimidating part are the numbers that are attached to each element. The numbers are creating paths on an invisible plain. ???? Follow me for a second please. Like I said before SVGs are created using the xml language. The SVG uses a coordinate system to define where paths are created within the given SVG. Elements within SVGs like path and circle use the coordinate system to define their shape.

Knowing this, we will take a look at one of the more complicated looking paths in the cartman SVG.

The d attribute within the path defines the shape that is about to be created. We also see a more familiar attribute called fill with a hex code value. This value will fill the shape with the color that the hex code produces.

OK, but …

That explains the way SVGs work but it doesn’t necessarily make creating a cartman that much easier. If would be extremely difficult to create paths using the d attribute. Fortunately for us there is a much easier way. There are programs that let you create vector elements and export those vectors into a SVG. The exported SVG can then be popped into your html document and bam you got your very first cute SVG!

Exporting an SVG

When creating an SVG in illustrator, we should make sure to pick the correct settings when exporting the svg. These our the settings that you want to go with.

When exporting SVGs from illustrator and most other svg makers you get code bloat that is safe to remove. I like to use Jake Archibald’s SVG optimizer. It strips out the code bloat and gives you an optimized version of the SVG.
https://jakearchibald.github.io/svgomg/

Cool Feature Bro - Scalable

One of the best features of SVG is it’s scalability. To appreciate this feature we must know why its so important. The issue with assets like jpegs, gifs, and pngs are that they don’t scale. If the asset is 300px by 300px then the asset cannot scale to a larger size without losing resolution aka looking shitty. Even if you downscale the image to 100px by 100px using CSS you will still lose quality. SVGs solve this problem. An SVG can be 100px by 100px or 100000px by 100000px and retain its quality. Plus the file size doesnt change so you can use the same 250 byte svg whether it be 100px or 100000px.

Cool Feature 2 Bro - CSS Editable

Another awesome feature of SVGs are that they are editable with CSS. You can use CSS to change colors and the size of SVGs. This is an incredible useful feature as it removes the need to have multiple versions of assets in different sizes and colors. Or the need to use an vector program to make minor tweaks.

With the ability to edit SVGs using CSS it gives us the ability to create awesome animations. Want to morph a circle into a square? You can do that! Want to make a spinning loader? You can do that! Want to morph Trump’s face to Hillary Clinton’s Face. You can do that! Actually not sure if you can do that but that doesn’t seem out of the question. We can animate SVGs using CSS3 animation properties like keyframes and animate.

Animate Time

We will start with a SVG I created. Just a real cute looking circle with multiple faces SVG.

Boom

Super simple example of animating an SVG, the same way you would animate any other html element.

Keep it responsive

Certain attributes need to be set on SVG elements for them to be responsive and scale appropriately. The viewbox and preserveAspectRatio are your Batman and Robin when it comes to creating responsive SVGs.

Remember how I said, SVGs use their own coordinate system to define where shapes are created and positioned. Well the viewbox is what defines that coordinate system. The viewbox takes four values: min-x, min-y, width, and height. The min-x and min-y values define where to start drawing on the coordinate system. The width and height define the amount of space available to us in the coordinate system. A simple and ideal responsive SVG would have the viewport values of 0 0 100 100.

The preserveAspectRatio defines how to enforce the aspect ratio. By leaving this attribute empty the value defaults to xMidyMid which centers the SVG within the viewport of the SVG, no matter the size the SVG has.

If you want to scale the SVG to the size of its container than we will need to use the image tag and set the src to the SVG file. The SVG should have the viewport set and the img element will resize the SVG to the containing element.

<imgsrc=”circle.svg”/>

Make it Accessible

Just how the img element has an alt attribute, SVGs can have elements within it that make them accessible for disabled web users.
Title and Description elements can more accurately describe your SVG while the aria attributes of aria-labelledby=”title” aria-describedby=”desc”, maximize screen reader support.

<svgversion="1.1"width="300"height="200"aria-labelledby="title desc"role="img"><titleid="title">Circle</title><descid="desc">A simple circle to show an SVG examlple.</desc><circlecx="100"cy="100"r="100"/></svg>

There is really a lot that you can do with SVGs and a lot to learn when implementing SVGs depending on your particular implementation. I plan on doing more tutorials on SVGs but I also highly recommend the following blog posts that go more into detail regarding SVGs.