Swapping Fill Color on Image Tag SVGs (Using CSS Filters!)
But SVGs on the web are far from perfect. One problem — not a fatal one, but still an issue with any virtual DOM — is that embedding SVGs directly into your app can be a resource hog. No matter how much you compress, no matter how logical and streamlined your components, if you need to load up hundreds of very-complex SVGs, React will need to track all of their nodes, and updating them becomes a chore. Changing properties on thousands of these nodes in real-time becomes noticeably laggy, mostly due to React having to continually calculate and batch-update so much material. This is especially pronounced in Safari 9.x and 10.x.
One way around this is simply linking to each SVG with an
<img> tag, instead of embedding the actual SVG in the DOM. This way, the virtual DOM only needs to track one node per image, instead of hundreds for each SVG:
But in doing so we’ve crippled our ability to manipulate our SVGs. No longer can we add stroke, move shapes, remove nodes or change fill. In short, if you want
:hover to change the fill color, you’re back in the stone age. On first blush the only solution would seem to be to load up a different color SVG! This is downright primitive. What are we? Cavemen??
The answer, and the reason I wrote this article, is that no, you don’t have to go back to the neolithic, because our forebears invented CSS
Although mostly used for gaudy blurs and overworked contrast levels, we can use these filter rules to — in effect — manipulate the SVG’s fill. All it takes is a bit of patience to get the properties just right, but it’s entirely possible.
Let’s say you want to change the fill color on a transparent-background SVG from black to blue on hover.
First, let’s target the image with a CSS rule and invert the color, from black to white, using
The amount you invert will be the lightness of the final color. For this shade of blue we’re inverting 50% (.5), giving us a medium-gray base to work with.
Now, the crux of the whole trick: If
filter provided us with some kind of
hue() property, this would have been much simpler. But no, as of 2017 this doesn’t exist, so we have to hack it. Instead, we can use
sepia() to turn our gray into a brown:
filter: invert(.5) sepia(1);
So here we are, finally with some color we can manipulate. Using
hue-rotate(), we can fiddle with these values until we arrive at the desired color. These can be visually adjusted until you get the right shade, or inspected in software like Photoshop and precisely reverse-engineered. To get a nice bright blue, I used these values:
filter: invert(.5) sepia(1) saturate(5) hue-rotate(175deg);
And that’s it! Check my CodePen example, and play around with it to see how you can manipulate the fill color. You now have a super-fast, CSS-only way to change the fill of an SVG, even in an
<img> tag, supported by every major browser (except IE). Enjoy!