Squeezing KB's from SVG's
Welcome to the ever evolving guide on how to optimize our SVG's for the web, ᴋeller style. The below is a very opinionated guide on squeezing every last possible bit of (kilo)byte from our SVG's before putting them on our website. Less KBs are always better. Performance baby, yeah!
I've used Adobe's Illustrator for quite a while, so this guide features it to optimize an SVG. However, I've found other software can do the steps described equally well:
Since writing this guide, I've had a lot of fun optimizing SVG's with Figma and SVGOMG , the latter being an fantastic PWA that performs most of the tasks listed below. Check it out!
Before we start, we need to make some changes to our Illustrator setup:
Set up Illustrator
In Illustrator, go to
Keyboard Shortcuts in the main menu. The following pop-up should appear:
We'll likely need to make our own keyboard shortcuts set. I've set mine up as "Jochem", but you can do whatever you prefer. Switch the dropdown to
Menu Commands and search for
bounds. There should be 1 entry listed, without a default keyboard shortcut.
Now add a keyboard shortcut for this command, because we'll be using it a lot. I have it setup to the following keyboard shortcut:
I prefer it that way, but again, you do you!
Congrats! We've set up Illustrator for effortless SVG manipulation. Now, let's optimize an SVG.
Open our SVG with Illustrator. It should look something like this:
If not, I recommend we set up our panels in a similar fashion to the screenshot above, for easier optimizing. The Layers panel is especially important going forward.
Strokes & fills
The first goal is to transfer the outline of a path to the fill of the path, for better cross browser compatibility. Review the bottom half of the left most panel. Does it look like this?
This means we have to transfer the color from the stroke to the fill. Do this by going to
Outline Stroke in the main menu. This should invert the stroke to the fill, like so:
Do this for every path that has a stroke instead of a fill.
Now it's time to ungroup and combine all paths. Review the Layers panel again:
Select everything with ⌘ + A, then ungroup everything with ⌘ + ⇧ + G. We can do this multiple times, until we see no more
Group's in the Layers panel. Our Layers panel should now look something like this:
Now we turn to the Pathfinder, to combine every path to the most minimal amount possible. With single color icons like the one in this example, our goal should be 1 path and 1 path only. For multi color icons the most ideal result is 1 path per individual color.
If we can't find our Pathfinder window, open it up via the main menu:
Pathfinder. Now, with all the paths selected, press this button:
This should merge close to all the paths and turn our Layers panel from this:
1 Group to rule them all. Now once more, ungroup everything with ⌘ + ⇧ +
G, which turns the Layers panel to the following:
Mostly nothing's changed right? In this example it didn't. But I can guarantee you this is a required step, so never skip it. Also, please review our SVG after these Pathfinder changes. Stuff can break. If I find an example of stuff breaking in the Pathfinder step, I'll add it to this guide. WIP, I guess.
Now it's time to turn all these paths to one single Compound Path (or individual Compound Paths for every path with a color). With all the paths still selected, go to the main menu and
Compound Path →
Release (or ⇧ + ⌘ + ⌥ + 8). This should turn our Layers panel from this:
Now create 1 single Compound Path by going to the main menu again and
Compound Path →
Make (or ⌘ + 8):
Great success! We now have 1 single Compound Path in 1 color.
Fit to Artwork Bounds
Remember the keyboard shortcut from the beginning? If not, here it is (in my instance): ⌘ + *
Go ahead and run it. The artboard should shrink to the icon like so:
Now it's time to export. Do this by going to the following menu item in the main menu:
Export As.... The following screen should appear:
Use Artboards option, give our icon a descriptive name and then press
Export. This following screen should appear:
Our options probably aren't the same initially, but make sure every option matches with the above screenshot. Click
OK, and we're done here!
As a last step, we can squeeze some last (kilo)bytes from our SVG by removing the fill. Open the SVG we recently exported in an editor like Sublime Text. We should see something like this:
fill="<color>" at the end there? Go ahead and remove it, since the color of our SVG's are handled by our CSS 99,9% of the time.
Final change before we're done; go ahead and change the
<title> value to something human friendly & readable. This greatly improves a11y (more about a11y ), which is always a win. So from the above example, change this:
To something like this:
<title>Icon of a lightbulb, indicating an idea</title>
The more time you invest in subtle changes like this, the more people will thank you for it later. You're making the web a better place, one SVG at a time 👍
We're done. Go ahead and add this optimized SVG to a website. There are several ways to do this, which I'll describe in a later blog.
Enjoy your squeezed SVG's! 🧙♂️