Favicons are the little icons you see in your browser tab. They help you understand which site is which when you’re scanning through your browser’s bookmarks and open tabs. They’re a neat part of internet history that are capable of performing some cool tricks.
One very new trick is the ability to use SVG as a favicon. It’s something that most modern browsers support, with more support on the way.
Here’s the code for how to add favicons to your site. Place this in your website’s <head>
:
<link rel="icon" href="/favicon.ico" sizes="any">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<link rel="manifest" href="/manifest.webmanifest">
And place this code in your site’s web app manifest:
{
"icons": [
{ "src": "/icon-192.png", "type": "image/png", "sizes": "192x192" },
{ "src": "/icon-512.png", "type": "image/png", "sizes": "512x512" }
]
}
Browsers that do support SVG favicons will override the first link element declaration and honor the second instead. Browsers that do not support SVG favicons but do support web app manifests will use the higher-resolution images. All other browsers fall back to using the favicon.ico
file. This ensures that all browsers that support favicons can enjoy the experience.
You may also notice the alternate attribute value for our rel
declaration in the second line. This programmatically communicates to the browser that the favicon with a file format that uses .ico
is specifically used as an alternate presentation.
Following the favicons is a line of code that loads another SVG image, one called safari-pinned-tab.svg
. This is to support Safari’s pinned tab functionality, which existed before other browsers had SVG favicon support. There’s additional files you can add here to enhance your site for different apps and services, but more on that in a bit.
Here’s more detail on the current level of SVG favicon support:
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
80 | 41 | No | 80 | No |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
126 | No | No | No |
Why SVG?
You may be questioning why this is needed. The .ico
file format has been around forever and can support images up to 256×256 pixels in size. Here are three answers for you.
Ease of authoring
It’s a pain to make .ico
files. The file is a proprietary format used by Microsoft, meaning you’ll need specialized tools to make them. SVG is an open standard, meaning you can use them without any further tooling or platform lock-in.
Future-proofing
Retina? 5k? 6k? When we use a resolution-agnostic SVG file for a favicon, we guarantee that our favicons look crisp on future devices, regardless of how large their displays get.
Performance
SVGs are usually very small files, especially when compared to their raster image counterparts — even more-so if you optimize them beforehand. By only using a 16×16 pixel favicon as a fallback for browsers that don’t support SVG, we provide a combination that enjoys a high degree of support with a smaller file size to boot.
This might seem a bit extreme, but when it comes to web performance, every byte counts!
Tricks
Another cool thing about SVG is we can embed CSS directly in it. This means we can do fun things like dynamically adjust them with JavaScript, provided the SVG is declared inline and not embedded using an img
element.
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<style>
path { fill: #272019; }
</style>
<!-- etc. -->
</svg>
Since SVG favicons are embedded using the link
element, they can’t really be modified using JavaScript. We can, however, use things like emoji and media queries.
Emoji
Lea Verou had a genius idea about using emoji inside of SVG’s text
element to make a quick favicon with a transparent background that holds up at small sizes.
In response, Chris Coyier whipped up a neat little demo that lets you play around with the concept.
Dark Mode support
Both Thomas Steiner and Mathias Bynens independently stumbled across the idea that you can use the prefers-color-scheme
media query to provide support for dark mode. This work is built off of Jake Archibald’s exploration of SVG and media queries.
<svg width="128" height="128" xmlns="http://www.w3.org/2000/svg">
<style>
path { fill: #000000; }
@media (prefers-color-scheme: dark) {
path { fill: #ffffff; }
}
</style>
<path d="M111.904 52.937a1.95 1.95 0 00-1.555-1.314l-30.835-4.502-13.786-28.136c-.653-1.313-2.803-1.313-3.456 0L48.486 47.121l-30.835 4.502a1.95 1.95 0 00-1.555 1.314 1.952 1.952 0 00.48 1.99l22.33 21.894-5.28 30.918c-.115.715.173 1.45.768 1.894a1.904 1.904 0 002.016.135L64 95.178l27.59 14.59c.269.155.576.232.883.232a1.98 1.98 0 001.133-.367 1.974 1.974 0 00.768-1.894l-5.28-30.918 22.33-21.893c.518-.522.71-1.276.48-1.99z" fill-rule="nonzero"/>
</svg>
For supporting browsers, this code means our star-shaped SVG favicon will change its fill color from black to white when dark mode is activated. Pretty neat!
Other media queries
Dark mode support got me thinking: if SVGs can support prefers-color-scheme
, what other things can we do with them? While the support for Level 5 Media Queries may not be there yet, here’s some ideas to consider:
- Use
light-level
to desaturate favicon colors in a low-light environment. - Use
inverted-colors
to “flip” inverted colors to preserve branding, or to ensure a photorealistic favicon looks as intended. - Use
prefers-reduced-motion
to remove favicon animation. Ideally, we’d avoid animating favicons in the first place, as they can be a trigger for ADHD and other related disabilities. - Use
forced-colors
and/or a Windows High Contrast Mode media query to make sure the favicon holds up visually in a high contrast color context. Remember to use color keywords to keep the favicon’s colors dynamic!
Keep it crisp
Another important aspect of good favicon design is making sure they look good in the small browser tab area. The secret to this is making the paths of the vector image line up to the pixel grid, the guide a computer uses to turn SVG math into the bitmap we see on a screen.
Here’s a simplified example using a square shape:
When the vector points of the square align to the pixel grid of the artboard, the antialiasing effect a computer uses to smooth out the shapes isn’t needed. When the vector points aren’t aligned, we get a “smearing” effect:
A vector point’s position can be adjusted on the pixel grid by using a vector editing program such as Figma, Sketch, Inkscape, or Illustrator. These programs export SVGs as well. To adjust a vector point’s location, select each node with a precision selection tool and drag it into position.
Some more complicated icons may need to be simplified, in order to look good at such a small size. If you’re looking for a good primer on this, Jeremy Frank wrote a really good two-part article over at Vidget.
Go the extra mile
In addition to favicons, there are a bunch of different (and unfortunately proprietary) ways to use icons to enhance its experience. These include things like the aforementioned pinned tab icon for Safari¹, chat app unfurls, a pinned Windows start menu tile, social media previews, and homescreen launchers.
If you’re looking for a great place to get started with these kinds of enhancements, I really like realfavicongenerator.net.
A funny thing about the history of the favicon: Internet Explorer was the first browser to support them and they were snuck in at the 11th hour by a developer named Bharat Shyam:
As the story goes, late one night, Shyam was working on his new favicon feature. He called over junior project manager Ray Sun to take a look.
Shyam commented, “This is good, right? Check it in?”, requesting permission to check the code into the Internet Explorer codebase so it could be released in the next version. Sun didn’t think too much of it, the feature was cool and would clearly give IE an edge. So he told Shyam to go ahead and add it. And just like that, the favicon made its way into Internet Explorer 5, which would go on to become one of the largest browser releases the web has ever seen.
The next day, Sun was reprimanded by his manager for letting the feature get by so quickly. As it turns out, Shyam had specifically waited until later in the day, knowing that a less experienced Program Manager would give him a pass. But by then, the code had been merged in. Incidentally, you’d be surprised just how many relatively major browser features have snuck their way into releases like this.
From How We Got the Favicon by Jay Hoffmann
I’m happy to see the platform throw a little love at favicons. They’ve long been one of my favorite little design details, and I’m excited that they’re becoming more reactive to user’s needs. If you have a moment, why not sneak a SVG favicon into your project the same way Bharat Shyam did way back in 1999.
¹ I haven’t been able to determine if Safari is going to implement SVG favicon support, but I hope they do. Has anyone heard anything?
Ah, this seems SUPER useful for something I’m working on right now – but here’s a question: is there any way to check browser support for this with JS? I imagine CSS.supports wouldn’t pick up on this at all (since it’s not CSS).
Oh great, I’m glad this can help!
I’m honestly not sure if you can check this sort of thing in JS. I’m assuming it’s not something you can target, as it’s just just a string in the
link
‘shref
attribute? I’d love to hear otherwise.I’ve been waiting to be able to use media queries via the
media
attribute. Not only would this allow us to avoid inline CSS in SVGs, but it would allow dark mode icons in maybe PNG if we do desire.Another neat trick are animated favicons: https://blog.tomayac.com/2019/12/01/animated-svg-favicons/. More of an experiment, though, probably don’t do this on a production site.
Shouldn’t the favicon.ico be linked before favicon.svg since it’s essentialy a fallback?
Tried implementing it on my site and Chrome seems to always pick the last one. Then again, that might by caused by it being a PNG file…
It happends to me too, even I have an icon file. It looks like Chrome has a preference for ico files, if I comment out the line with the link to the ico file, the browser shows the svg file.
Urgh I’m getting this too, why on earth is Chrome doing this. If I have the favicon.ico file at the root and delete the line, it still defaults to the .ico file and not the .svg. Bloody bonkers.
This is helpful, thanks a lot.
impressive amount of resource on the topic, great article!