When making a layout from PSD, icons are often inserted in SVG format, and if not, we ask them from the designer. Earlier we used icon fonts, but recently saw the advantages of sprites and decided to try to play with them to place them into the development process. We like icon fonts, but they have a number of disadvantages (you can read CSSTricks on this topic CSSTricks). The experiment was successful, and that’s how we have organized the system.
Conditions
What we need from sprites:
Flexible management of the size, colour and behaviour (hover, focus, etc.) of an icon
Automation, minimum of manual work
Loading only the necessary icons on the page
Easy insertion of icons into page layout (I use pug for templating html)
Folder structure:
You can read and learn more about how our build works. in the repository.
To create a sprite, use gulp, namely:
gulp-svg-sprites - sprite creation
gulp-svgmin - SVG minification
gulp-cheerio - removal of unnecessary attributes from svg
gulp-replace - fixing some bugs, about it we will tell below
Let’s go
Install the plugins (we do this globally and then link):
Unveil the plugins in the gulpfile:
Cooking sprite
The first task is to create an html file with symbol tags.
Let’s see what is happening here in parts.
We point where we need to get the icons and minify them. The variable assetsDir is for convenience.
Remove the style, fill attributes from the icons so that they do not interrupt the styles specified via css.
However, this plugin has one bug - sometimes it converts the symbol ‘>’ to the encoding ‘>’.
This problem is solved by the following piece of task:
Now we will make a sprite from the one we have got and put it in a folder:
symbol_sprite.html - is our sprite. Inside it will contain the following (for simplicity, a couple of icons is shown):
Pinch styles
Now we need to make styles for our sprite (in this case the .scss file). We can specify this file in the gulp-svg-sprites plugin, but what a nuisance - it cannot be done with this setting:
Let’s make a separate task for creating scss.
In the cssFile property we declare where to put the file on the scss (then we include it).
In the templatesproperty we declare where to get a template for it. Template code:
We get the _svg_sprite.scss as follows:
The compiled css will be like this:
Please note that the sizes of the icons are expressed in em, which will allow us to manage them through font-size.
Make the final task, and run it:
Connect to the page
So, we have got an html-file with icons and a scss-file with the design. Next, connect the file to the page using caching via localStorage. This process is well described in the article Caching SVG Sprite in localStorage.
Connect the js-file as follows:
The file is connected and it is cached after the first boot. If you need to update the sprite in the js-file above, change the revision parameter to +1.
We insert the icons through the mixin pug, because it is fast and convenient:
Now, in order to embed an icon we fetch a mixin with its name:
Final html:
Open the page in the browser:
For now, the sizes of the icons are in full size and have a standard colour. Let’s change this (not in the generated file, but in the main file):
Result:
That’s it, we’ve got a working system for connecting icons via sprites, but there is one more thing.
Blurring
Unfortunately, not all designers make icons on a pixel grid. In this case, the icons will be “blurred”. If you export icons from the illustrator, you need to initiate the pixel grid and adjust the size and location of the icon to the pixel grid. If you work in ready-made svg-files, use the iconmoon service iconmoon to properly align them.