How I Made an Icon System Out of CSS Custom Properties
There are many ways to include an SVG on a page and each technique has its own advantages and disadvantages. For the last couple of years, I have been using a Sass function to import directly my icons in my CSS and avoid having to mess up my HTML markup.
What I have for you here is a Sass function that creates a SVG icon library directly in your CSS.
The SVG source code is compiled with the Sass function that encodes them in data URI and then stores the icons in CSS custom properties. You can then use any icon anywhere in your CSS like as if it was an external image.
This is an example pulled straight from the code of my personal site:
This technique has both pros and cons, so please take them into account before implementing this solution on your project:
I mostly use this technique for icons rather than logos or illustrations. An encoded SVG is always going to be heavier than its original file, so I still load my complex SVG with an external file either with an
<img> tag or in my CSS with
There are two ways to use an SVG in your CSS with data URI:
Here is a basic example showing how you how to use those two methods:
The main issue with this particular implementation is that you have to convert the SVG manually every time you need a new icon and it is not really pleasant to have this long string of unreadable code in your CSS.
This is where Sass comes to the rescue!
By using Sass, we can make our life simpler by copying the source code of our SVG directly in our codebase, letting Sass encode them properly to avoid any browser error.
Here are the four steps of this technique:
If you have followed those steps, Sass should compile your code properly and output the following:
The now-implemented Sass
svg() function works great. But its biggest flaw is that an icon that is needed in multiple places in your code will be duplicated and could increase your compiled CSS file weight by a lot!
We will keep the same code we had before, but this time we will first output all the icons from the Sass list into the root of our webpage:
Now, instead of calling the
svg() function every time we need an icon, we have to use the variable that was created with the
This technique does not provide any optimization on the source code of the SVG you are using. Make sure that you don’t leave unnecessary code; otherwise they will be encoded as well and will increase your CSS file size.
With this technique, we cannot select with CSS specific parts of the SVG. For example, there is no way to change the
fill color of the icon when the user hovers the button. But there are a few tricks we can use with CSS to still be able to modify the look of our icon.
For example, if you have a black icon and you want to have it white on hover, you can use the
invert() CSS filter. We can also play with the
Another trick to be able to change the color of your icon, is to use it as a mask on your pseudo-element with a background. Set your pseudo-element as
inline-block with a
background-color and define a
height for the size needed.
Once you have a rectangle with the color needed, apply those four values to only keep the shape of the SVG needed:
Don’t forget that all CSS
mask properties still need to be prefixed with
-webkit- for most browsers as of September 2022.
I hope you find this little helper function handy in your own projects. Let me know what you think of the approach — I’d be interested to know how you’d make this better or tackle it differently!
If you need help creating a digital marketing strategy for your business, don’t hesitate to contact one of Digidude’s consultants.