How to use SVG Sprite Icons for development
Contents
Intro
In computer graphics, a texture atlas (also called a spritesheet or an image sprite in 2D game development) is an image containing multiple smaller images, usually packed together to reduce overall dimensions. An atlas can consist of uniformly-sized images or images of varying dimensions. A sub-image is drawn using custom texture coordinates to pick it out of the atlas.
In web development, an SVG sprite is a collection of SVG icons or graphics bundled together in a single SVG file. Each graphic within the sprite can be referenced individually, allowing for efficient use of SVG graphics on a website. SVG sprites are particularly popular for icons, logos, and other small graphics that can be reused across a site.
NPM Packages
We will need to install a few node packages:
All of this (and types!) can be installed with one following command, where -D
it's --save-dev
so don’t worry about production build.
We actually only need one package, but we'll be cool developers and install them all, read on to find out why.
SVG Icons
Let's download a set of icons for the sprite, I prefer to use Bootstrap Icons
After getting icons arrange files something like this:
Now it's a time to use already installed packages 🔥
Making Sprite
Here is the simple script below where we using prompts and svg2sprite-cli
- prompts package is responsible for command line interaction with the user, we will use only select as an prompt type, there are a lot of additional types and you can read about all of them in the documentation
- svg2sprite-cli we will use this package to create a sprite, just pass the input and output values to it, add extra parameters like
--stripAttrs
if needed, again, if you are interested, read the documentation
You can add more than one of choices to have a multiple sprites:
Now you can run this script with node sprite
and you will see a select CLI input from the prompts package, by selecting the appropriate item your sprite will be assembled ✅
Update index.html
Now need to inject it to the project into index.html
with help of jsdom and prettier
Here is the index.html example below, pay attention to data-sprite="bootstrap"
this is the element where our sprite will appear:
Let's update sprite.js script with the next code:
- jsdom this package will be used to work with DOM and insert sprite into index.html
- prettier this package will be used for text formatting
Again, you can run this script with node sprite
at this time the generated sprite will be inserted into the index.html
, the contents will be formatted and the sprite is completely ready to work!
From now at any time anywhere you can display icons from sprite as follows, just specify the link to the icon name #bookmark
like. Using attributes you can adjust the size, and you can also apply the fill (if earlier you cut this attribute from the original file using --stripAttrs
)