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.