SVGBOX: Icons as Images, With a Color Parameter

A pretty good icon setup, as the long-standing advice goes, is simply inline <svg> dropped in wherever an icon is needed. It is easy, keeps full design control, performs well in general, and avoids wrestling with caching and browser-support issues.

Using <img> for icons is a reasonable alternative. Fine-grained design control is more limited (though filter can still recolor them), and it is arguably not quite as fast since the images must be fetched separately from the document — but many of the inline approach's advantages carry over.

What the project adds

Shubham Jain's SVGBOX serves icons as <img> and removes one of the design-control constraints by accepting a URL parameter for color. An Instagram icon in red, for instance, is requested by passing in red.

For anyone using many icons at once, the copy-and-paste code SVGBOX provides includes an "SVG sprite" variant built on a URL like this:

<img src="https://s.svgbox.net/social.svg?fill=805ad5#instagram">

That variant increases the download weight, since the whole icon set is fetched, but it may be more efficient by turning many downloads into one — a trade-off that is hard to call in the HTTP/2 era.

The #instagram fragment at the end of that URL looks like an ordinary hash link, but it is a fragment identifier in SVG terms: it displays only the portion of the SVG matching the relevant <view> element. That is not a technique you see every day.

Direct Link →