Combine Multiple SVGs into an SVG Sprite

Damon Bauer
InstructorDamon Bauer
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 6 years ago

In this lesson, we’ll explore the process of combining all of your SVG icons into one SVG sprite, to be included directly into markup. We’ll use the command line, along with the svgo and svg-sprite-generator npm packages to automate the process of compressing the SVGs and creating the sprite.

Note: This lesson assumes you have npm installed.

[00:01] Let's start in the terminal with installing svgo, and svg-sprite-generator by typing NPM install -g for global, svgo svg-sprite-generator. With these packages installed, we'll start by typing svgo, we'll pass the -f flag for folder, and I'll pass the path to the folder containing the original SVGs.

[00:27] The -o flag for output folder, and a path to the folder that should contain the optimized SVGs. I'll add two ampersands, which tells the command line to run the proceeding command only if the previous command finished successfully.

[00:43] Anything we type on this side of the && will only run if this side completed successfully. Now I'll type svg-sprite-generate, I'll pass the -d flag for directory, and I'll pass the path to the folder containing the optimized SVGs.

[01:01] I'll pass the -o flag for output file, and I'll pass a path where I want the SVG sprite to be written to. I'll run this command and see that SVGO responded with each file it found, and its compression results. The result of this command is a single chunk of SVG markup that has multiple symbol elements with unique IDs, which you can use to style via CSS.

egghead
egghead
~ 16 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today