How to Create and Manage SVG Sprites

Default avatar.
May 04, 2017
How to Create and Manage SVG Sprites.
SVG is winning the race when it comes to graphics that scale on the web. Designers and developers are choosing SVG in lieu of icon fonts, raster images, and raster sprites. The benefits of using SVG are numerous but to me, it boils down to scalability. Using SVG can be as simple as copying and pasting the exported code from a vector-based program. From there the problem lies with delivering those graphics in the most efficient manner. Enter SVG sprites. These work in a similar pattern as image sprites do though, how they are created and included on a web page is very different.

Why Use Sprites at all?

Sprites help increase speed, maintain a consistent development workflow, and make the creation of icons much faster. SVG sprites are typically created using icons of a similar shape or form whereas larger scale graphics are one-off applications. In many cases, an icon library will scale in size. Adding new icons needs to be efficient and ultimately easy. SVG sprites help make this a reality.

Exporting SVG Code

SVGs can be exported from your favorite vector graphic program. I tend to use a mix of tools and have found that all of them have great support for exporting SVGs. Sketch stands out in particular because you can select an icon or graphic and hit command + c and have that SVG code copied to your clipboard. Then you can head to your code editor, paste the code, and have the graphic appear before your eyes in the browser.

Optimizing SVG for Web

Sadly, if you were to copy and paste from Sketch you may find that the code is suboptimal and could be better optimized. There are some tools for that. If you use Sketch in particular, check out the SVGO Compressor plugin. As you export SVGs the plugin automatically optimizes them before saving the file. If you don't use Sketch check out the same functionality in app form or if you prefer web apps, Jake Archibald put together one for you.

Creating SVG Sprites Manually

Creating an SVG sprite can be done manually. You will need a vector design program that can generate SVG code. For this, I'll make use of Sketch and export a few icons as SVG. JiahjZD After exporting the icons and running them through the SVGO Compressor plugin I'm left with the following for each icon. Note that at this point each icon is a separate file:
<!-- aircraft.svg -->
<svg xmlns="https://www.w3.org/2000/svg" width="62" height="51" viewBox="0 0 62 51">
 <path fill="#000000" d="M38.9872..."></path>
</svg>

<!-- attachment.svg -->
<svg xmlns="https://www.w3.org/2000/svg" width="60" height="64" viewBox="0 0 60 64">
 <path fill="#000000" d="M15.9264..."></path>
</svg>

<!--brush.svg-->
<svg xmlns="https://www.w3.org/2000/svg" width="62" height="62" viewBox="0 0 62 62">
 <path fill="#000000" d="M7.8416..."></path>
</svg>

<!--camera.svg-->
<svg xmlns="https://www.w3.org/2000/svg" width="64" height="52" viewBox="0 0 64 52">
 <path fill="#000000" d="M32,19.2 ..."</path>
</svg>
Now that our SVGs are all optimized we can begin creating the sprite using the code above. To create a sprite start with the surrounding <svg> element which will serve as a container for all the icons. This element will need to be within your document similar to the code below:
<!DOCTYPE html>
<head>
 <title>SVG Sprites</title>
</head>
<svg style="display:none;">
 <!-- icons will go here -->
</svg> 
</html>
Notice how the SVG has an inline style of display:none. This is required to properly utilize SVG sprites. Even though the icons won't render on the page a large amount of space will. To avoid this extra space the SVG creates, we are hiding the SVG with CSS. Simply including the code from the icons won’t suffice at this point. We need to make use of a <symbol> element which will allow you to embed the SVG code as well as supply it’s own specific viewBox attribute which is important for icons that might display at different widths and heights. Elements defined inside the <symbol> element can only be rendered to the browser when referenced by the <use> element. The final sprite resembles the following snippet:
<!DOCTYPE html>
<head>
 <title>SVG Sprites</title>
</head>
<svg style="display:none;">
 <symbol id="aircraft" viewBox="0 0 62 51">
 <path fill="#000000" d="M38.9872..."></path>
 </symbol>
 <symbol id="attachment" viewBox="0 0 60 64">
 <path fill="#000000" d="M15.9264..."></path>
 </symbol>
 <symbol id="brush" viewBox="0 0 62 62">
 <path fill="#000000" d="M7.8416..."></path>
 </symbol>
 <symbol id="camera" viewBox="0 0 64 52">
 <path fill="#000000" d="M32,19.2 ..."</path>
 </symbol>
</svg> 
</html>
Notice how I've added id attributes to each symbol element. This is important because when you reference a specific icon in the sprite you need a unique way to target it. Also, notice how each icon has it’s own unique viewBox parameters. The first two parameters will almost always be "0 0"; the second two will be equal to the size of the icon you have exported.

Including an icon on the page

With the sprite all ready to go we need a way to include specific icons on a page. To do this you will utilize the <use> element written like this:
<svg>
 <use xlink:href="#aircraft"></use>
 <svg>
On your page, you should see the icon appear and that’s really all there is to it!

Automating SVG Sprites

Creating inline SVG sprites isn't all that hard. The problem lies with the fact that many developers need to create sprites for hundreds of icons or more for robust applications. Creating something like that by hand could get time-consuming and tedious. Developers need a way to automate the process so they aren't wasting time creating sprites by hand. Luckily, there are tools for such a thing. Task managers such as Grunt or Gulp have plugins available to use. These streamline the creation of the SVG sprites. By supplying each SVG file within a given directory the plugin will both optimize and generate all the SVGs in your referenced directory to be included in a fresh new sprite. If you need to add more icons to the sprite all you really have to do is export the icon as an SVG and add it to the same folder. From there the plugin handles the time-consuming part. Some available plugins in different flavors: grunt-svgstore, gulp-svg-sprites, svg-sprite.

Conclusion

As with all things in web development, there is more than one way to create SVG sprites. You can reference them externally, include them via CSS, and even make them responsive.

Andy Leverenz

Andy Leverenz is a co-founder and technical director at Couple of Creatives, a small branding agency from St. Louis, MO. He spends his days designing and developing websites and apps. When he’s not working, he plays guitar for fun in two bands. Check out his side project, Web-Crunch, an online publication where he writes and delivers news for designers, developers and entrepreneurs across the globe.

Read Next

6 Best AI Productivity Apps in 2024

There’s no escaping it: if you want to be successful, you need to be productive. The more you work, the more you…

3 Essential Design Trends, February 2024

From atypical typefaces to neutral colors to unusual user patterns, there are plenty of new website design trends to…

Surviving the Leap from College to Real-World Design

So, you’ve finished college and are ready to showcase your design skills to the world. This is a pivotal moment that…

20 Mind-Bending Illusions That Will Make You Question Reality

Mind-bending videos. Divisive Images. Eye-straining visuals. This list of optical illusions has it all. Join us as we…

15 Best New Fonts, February 2024

Welcome to February’s roundup of the best new fonts for designers. This month’s compilation includes some innovative…

The 10 Best WordPress Quiz Plugins in 2024

Whether it’s boosting your organic search visibility or collecting data for targeted email marketing campaigns, a great…

20 Best New Websites, February 2024

It’s almost Valentine’s Day, so this latest collection is a billet-doux celebrating the best of the web this month.

Everything You Need to Know About Image Formats In 2024

Always trying to walk the tightrope between image quality and file size? Looking to branch out from JPGs and PNGs this…

The 10 Best Logos of 2023 - Ranked

From vintage aesthetics to innovative new color schemes, we’ve seen a lot of creative logo designs this year. In this…

Exciting New Tools for Designers, February 2024

2024 is well underway and shaping up to be an interesting year. So, to help you keep your focus on working hard and…

The Art of Engineering AI Prompts

In the rapidly evolving world of artificial intelligence, the ability to communicate effectively with AI tools has…

15 Best New Fonts, January 2024

In this month’s roundup of the best new fonts, we find a large number of vintage, retro, and revival typefaces. Is it…