Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Combine Multiple SVGs into an SVG Sprite


    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.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    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.

    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.

    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.

    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.