Something I used to struggle with was exporting SVG from Adobe Illustrator. Our designers make these great comps for us to code up. But what’s the best way to take their vector files and convert them into an optimized SVG on the web?
This is exactly what you will learn today.
Creating SVG in Adobe Illustrator
When you create a graphic in Illustrator—which is intended for SVG export—you need to perform some steps and considerations required to make the final output properly optimized for web. Let’s explore them now.
Set the Correct Color Space
Illustrator — as most vector illustration software — was originally designed for print production, and therefore its color space is set to CMYK by default. RGB is much more appropriate for web and screen use, and has a wider gamut (range of colors) than CMYK. So, when you create new document, make sure the color mode is set to RGB – as you can see in the image below.
Give your drawing a proper structure
An SVG file is not like a regular bitmap image — a grid of pixels. It’s a text document that has a specific structure.
Like an HTML document, you can select and manipulate individual elements separately. To do this, you’ll need to use their names as a reference. I’ve found it’s always much easier to create these labels during visual editing in Illustrator, rather than later.
For that reason, it’s important to give a meaningful name to each graphic element as you make it. Here is what you need to know when you create a graphic in Illustrator:
- Layers and Layers Groups names are used as IDs for SVG groups
- Symbols names are used as IDs for SVG symbols
- Graphic Styles names are used as CSS classes
In the images below, you can see in action how names from an Adobe Illustrator file reflect to the exported SVG
Simplify your shapes whenever possible
The shapes in an SVG drawing are described with coordinate points. The more points a drawing has, the larger the file size and more difficult it is to edit and maintain. Creating small, efficient files makes your life easier later.
To solve this issues, you need to use the fewest possible number of points to create the shapes you need. This can be achieved in several ways.
Use Primary SVG shapes instead of SVG paths whenever possible
Using simple elements like line, rect, and circle has some significant advantages.
Firstly, simple shapes are much more readable for humans – it’s self-evident that a circle is a circle when we see it in our SVG code, but the code for a path can be anything until we see it render.
Secondly, simple shapes almost always produce smaller file sizes and less code, which make them easier for maintain and edit. You can also control them more easily with their direct attributes such as x, y, cx, cy, instead of point coordinates as it is with paths.
To see what I mean, in the image below you can see a simple circular shape defined once as a SVG circle element and once as a SVG path. Though they render identically, it’s clear that the SVG shape is smaller and more versatile. Be aware that some graphics editors – Fireworks’s SVG export extension was an example – automatically convert SVG shapes to paths when you export. Obviously, try hard to avoid this.
Simplify your paths
A path is nothing more than an array of coordinate points. To simplify a path means to cut out some of its points, which will lead to less path data and smaller file size. To do so you can use Object > Path > Simplify… command or Warp Tool. In both cases, the main point is to reduce the path’s points maximally without loosing the quality of visual appearance.
In the images below, you can see how Illustrator’s simplify process reduces path points from 32 to 23 — which is about 25% — and how this reflects to the code. The path data is decreased while the visual quality still remains at a good level.
Decide whether to convert text to paths
In SVG graphics, text is a standalone element and as such it is searchable, accessible, and easily re-editable. This is a valuable quality for text to have. However, if you want to guarantee your text looks exactly the way you designed it everywhere, your end-user will need to have access to the correct fonts. This means choosing a common fonts — which limits your creativity — or providing a web font.
If precise text rendering is more important than editability — for example, in a logo — you can convert it into paths by using Type > Create Outlines command or by setting this option in the export panel as you’ll see later on.
Bear in mind that convert a lot of text to paths, the file size can increase drastically – so think carefully before converting.
Use ‘SVG filters’ in preference to Adobe Illustrator or Photoshop Filter Effects
Illustrator offers a set of SVG Filters that are applied live in the browser (Effect > SVG Filters). While Illustrator or Photoshop Effects are always permanently ‘baked into’ your raster images embedded inside the SVG, SVG filters can be changed or removed at any time with a few keystrokes.
You can also create re-usable filters and/or edit them via Apply SVG Filter dialog.
Fit artboard to drawing
If you want your SVG to be displayed predictably, it’s good habit to always to trim your artboard to the drawing before. The artboard dimensions are the dimensions of the exported SVG viewport, and any white space in the artboard will be generated as white space inside the viewport.
Depending on situation you can use Object > Artboards > Fit to Artwork Bounds or Object > Artboards > Fit to Selected Art command.
- Left star will be exported with the surrounding white space
- Right star will be exported with its proper dimensions.
Exporting SVG in Adobe Illustrator
When your graphic is ready for production, select File > Export > Export As… command, then select SVG as option for the file type and hit Export button. You should see the following panel:
Let’s explore the options presented in more detail.
There are three way to style your SVG and they are presented in the first dropdown list.
- The first is to use internal CSS (i.e. a block), which is generally considered the best option following the Separation of Concerns principle.</p>
- The second method is to use inline CSS styles (i.e. ).
- And the third method is to use SVG presentation attributes .
In the image below, you can see the difference between these three options.
If you want to convert your text to outlines, here you can instruct Illustrator to do so. If you want to preserve your text editability, then select SVG option. Outlined text gives you complete visual control of your typography, but at a significant cost – file sizes blow out and text loses editability and searchability.
Note: SVG fonts will be removed from SVG 2 and is considered as a deprecated feature with support being removed from browsers.
If you are using a raster graphic (like a JPG) in your SVG, you have two options. Choosing link means the raster graphic will live in its own file somewhere else. Choosing embed will make it part of the SVG. There’s no real performance difference either way since the file still has to be loaded. But linking keeps your SVG and non SVG components separate and easier to read. This one is personal preference. I like to embed it unless it’s a raster graphic I’m reusing elsewhere.
As a general rule, you’ll avoid many future headaches if you can simply avoid using pixel-based graphics in your SVGs whenever you can.
Generally the best option is to select Layer Names, because this will give you meaningful names for your individual SVG elements. Minimal uses random letter-numbers, and Unique uses large random combination of characters.
As you can see from the graphic below, the path with 5 decimal places if far larger than the same path with 1 decimal – yet visually identical.
Keep in mind that If you make the number smaller, you’re removing precision of your vectors. If you make the number bigger, you’re adding precision but also adding to the file size.
Check this option only if you’re exporting a final version of your graphic for production and you are sure that the file won’t be edited anymore.
It might make logical sense to turn this on. But we actually want to leave it off. If we check this box, our SVG won’t have any width or height on it. Until we add a width and height with CSS, our SVGs won’t know what to do. In a lot of cases, they will fill as much space as possible. By turning off responsive, we are adding a width and height to the base SVG. Luckily, if we also define a width and height in our CSS, it will override what is defined in the SVG itself.
However, you may want to further optimize your file with some specialized tool, such as SVGO or its web GUI-fied version SVGOMG.
Be aware that you do need to be careful when using these tools. They can easily break your document functionality. My advice is to use manual optimization whenever it’s possible and use automated optimization tool only if it’s necessary and with caution.
Now that you have your SVGs optimized and ready for use on the web, you’re all set to use them. Unlike other image files, there’s a couple different ways to get your vectors onto your page. I gave a talk at Connect.
Keen to learn the ins and outs of developing your own plugins, either to improve your workflow or the workflow of others? Check out WHAT IS SVG?