Ever heard of favicons made with SVG? If you are a regular reader of CSS-Tricks, you probably have. But does your website actually use one?
The task is more non-trivial than you might think. As we will see in this article, creating a useful SVG favicon involves editing an SVG file manually, which is something many of us try to avoid or are uncomfortable doing. Plus, we are talking about a favicon. We can spend a few hours playing with a hot new CSS framework. But a favicon? It sometimes feels too small to be worth our time.
This article is about creating an SVG favicon for real. If you’re in front of your laptop, get the vector file of a logo ready. By the end of your (active!) reading, your SVG favicon will be ready to shine.
Why an SVG favicon at all?
We are here for the “how” but it’s worth reflecting: what is an SVG favicon even good for? We already have other file types that handle this, right?
SVG is a vector format. As such, it has valuable features over raster formats, including those typically used for favicons, like PNG. SVGs scale and are often more compact than its binary cousins because, well, they’re just code! Everything is drawn in numbers and letters!
That’s good news, but how does this help our favicon? Desktop favicons are small, at most 64×64. And you can ship your icons in several resolutions. Scaling is a feature we don’t really need here.
File size is another source of disappointment. While SVG has a clear edge over a high resolution PNG file, the tables turn in low resolution. It is common for a 48×48 PNG favicon to result in a smaller file size than its SVG equivalent.
Yet, we have a good reason to pay attention to SVG favicon: dark mode support.
Dark mode has received a lot of attention recently. You may even have implemented dark mode for your own websites. What does that mean for favicon? It means the ability to show different icons based on the brightness of the browser tab’s background.
We are going to prepare such an icon.
How to create an SVG favicon (in theory)
Getting dark mode support for an SVG favicon relies on a CSS trick (10 points to Gryffindor), namely that we can drop a <style>
tag right inside SVG, and use a media query to detect the user’s current theme preference. Something like this:
<svg>
<style>
@media (prefers-color-scheme: dark) {
// Your dark styles here
}
</style>
<!-- more stuff -->
</svg>
With this pattern, your light/dark favicon is only limited by your imagination. For example, change the color of all lines:
<svg>
<style>
path { fill: black; }
@media (prefers-color-scheme: dark) {
path { fill: white; }
}
</style>
<!-- more stuff -->
</svg>
Now is the time to actually write these styles. This is when the troubles begin.
SVGs are images, and the logo we are using to build our favicon was probably created with a tool like Adobe Illustrator or InkScape. Why not use the same tool again? That’s because apps like these haven’t really integrated CSS and media queries into their products. It’s not that they can’t handle them, but you have to forget the mouse-only experience they promise. You are going to use the keyboard and type code.
Which leads us to a second option: write the CSS by hand. After all, this is the way to go with front-end development. Why should it be different here? Unfortunately, SVG is often hard to read. Sure, this is an XML dialect, which is (almost) like HTML. But SVG images are cluttered with long path
declarations and other markup we often don’t see in our day-to-day work. For example, the Raspberry Pi logo is more than 8KB of raw data. This make manual editing more tedious than it sounds.
How to create an SVG favicon (in practice)
To understand how we can deal with an SVG favicon quickly and easily, we first need to define what we want to achieve.
The technique we covered above calls for creativity: replace a color, invert them all (which we’ll get to), change a shape… But the setup for a favicon is not the right time for this. A favicon should almost always be the website’s logo. Its appearance? Aesthetic? The message it conveys? All these questions have been answered already. Preparing the favicon should be fine-tuning the logo so it fits the small space allocated in browser tabs.
Often, the logo is perfect as-is and its favicon counterpart is a scaled down version of it. Sometimes, we need to add margin to make it square. What motivates a dark icon, then?
Contrast.
Many logos are designed for light backgrounds. When they don’t, another version exists solely for the purpose of darker backgrounds.
Therefore, whether we prepare a favicon manually or with a tool, we automatically pick the light-compatible logo and start with that. After all, desktop tabs are traditionally white or light gray. The problem arises when going dark mode.
Now that we have pinpointed the problem, we can formulate a solution: sometimes, we need a brighter icon for dark mode. It’s very simple. For a colorful, yet too dark logo, we can add brightness to a dark mode favicon with a CSS filter:
<svg>
<style>
@media (prefers-color-scheme: dark) {
:root {
filter: brightness(2);
}
}
</style>
<!-- more stuff -->
</svg>
If the logo is in shades or gray, we can invert its colors using another CSS filter:
<svg>
<style>
@media (prefers-color-scheme: dark) {
:root {
filter: invert(100%);
}
}
</style>
<!-- more stuff -->
</svg>
Your turn! Open your SVG logo in a text editor and drop any of those <style>
snippets above just before the closing </svg>
tag. Open your logo in a browser, switch from light to dark, then from dark to light (Windows or Mac), and observe the magic. Adjust the brightness
or invert
filters as needed.
How fast was that?
Even faster: The SVG favicon editor
That brightness hack we covered didn’t come out of nowhere. I wrote it while upgrading RealFaviconGenerator with the SVG favicon editor. This online tool includes everything we discussed earlier. Submit your SVG logo to get a preview of your favicon in tabs and Google result pages.
After that, play with the editor to produce the perfect favicon. In this example, we make the dark icon lighter, using the brightness
filter hack behind te scene:
Grayscale logos benefit from the invert
filter as well:
Click on the “Generate Favicon” button, et voilà! Favicon ready, fine tuned for light and dark modes in under a minute. Mission accomplished.
Conclusion
Beyond coolness, SVG favicons actually solve a problem that its PNG counterpart cannot. It’s only been about a year since we’ve had the ability to use SVG this way at all, but so far, it seems seldom used. With intentional purpose and tooling, maybe SVG favivons will rise and find its place among the favicon classics.
I’m using an SVG favicon on my personal portfolio that has the background dynamically updated as you scroll – so there’s a gradient that matches where you are on the page as you go down. Was pretty fun to implement and a neat little trick.
That’s a nice trick!
Awesome trick
That is neat. Where can we see it in action?
his portfolio ;) https://kyleconrad.com/
I wish people would stop spreading this misinformation.
(prefers-color-scheme: dark)
does not mean that your favicon is being rendered against a dark background (though in practice it’s almost certain to be), and its absence certainly does not mean that your favicon is being rendered against a light background.prefers-color-scheme
is about content, not chrome, but the favicon will be being rendered against browser chrome, not content colours. By default, these mostly line up on most platforms now, but only by default, mostly, and on most. As examples where it doesn’t line up: Chromium incognito windows unconditionally get dark chrome; and Firefox and Chromium both support themes that could cause the favicons to be rendered onto any surface at all (not even just one colour).Your favicon needs to work against near-white and near-black colours, and to not be too awful against any surface.
(prefers-color-scheme: dark)
is a hint that it’s almost certainly rendering against a dark colour, but you mustn’t rely on it. Pragmatically it’s OK for(prefers-color-scheme: dark)
to only work against darker colours, but it’s not OK for the default to not work against dark colours.I backup what you say, especially regarding incognito mode. It looks like it could be considered dark but is not.
I will update RealFavicon’s editor to reflect this so people understand what’s going on.
I think there could be many more use cases than just dark mode.
For example, with SVG, it’s easy to programmatically add or remove elements. You could implement status icons or activity indicators, insert them into the SVG’s DOM using JavaScript, then convert to a data URI and change the favicon on the fly, all without ever leaving your text editor.
So GLAD to read an article about using SVG that doesn’t require JavaScript.
Great article, thanks for sharing!
Thank you Ricardo!
Thank you for writing this!
Are web services and crawlers that pick up favicons for search results and such, affected by using .svg instead of .ico for favicons?
The first code example targets path. The others target :root. What is the difference?
Will this inline style hack pick up user preferences if the SVG is NOT being used as a favicon (an SVG logo on a regular web page, for example)?
Can the user preference style be written in an external CSS style sheet instead of in the SVG file itself?
Thanks again!
I don’t know about web services or crawlers but I can answer the other questions:
With SVGs, path is one element in the rendered image; :root will target all paths. So if you imagine a button, the background rectangle will be one path, as will each letter. You can target each path individually, or wrap them in a group and change them all at once (but means editing the SVG).
Yes, inline style will work whether it’s a favicon or a logo. You can even embed @keyframes inside the style tag in the SVG.
These styles can be written in an external CSS style sheet IF the SVG is inline. If you’re calling the SVG via or as a background-image, you’ll need to embed the styles in the SVG themselves. I’m not sure why this is—maybe because the browser can’t read the SVG’s DOM and thus, can’t apply the styles? (@CSS-Tricks, article idea!)—but embedding the styles in the SVG file itself does the trick.
Nice, this worked well! Thanks for sharing.
Anyone got to get a link with example, I thought I can download?
I tried using the SVG Favicon Generator, but no SVG I uploaded would show up in their web interface. Simple SVGs exported from Vectornator — I figured the file I was using was too big so I scaled back the size to 48px square and still, nothing showed up. No errors, no nothing, just a tool showing a blank favicon that let me adjust its brightness, to no avail.
Seems like a cool idea, though. I’m probably doing my SVGs wrong somehow.
Have honestly never been so proud of such a small little detail on my site before! Implemented it to match the colours of light or dark mode on the site, as applicable. I do think it looks great, but I guess a miniscule amount of people will ever actually notice that it changes. I do notice also that on Chrome/Windows it isn’t instant – you have to change OS light/dark mode, then hit refresh in your browser.
Not supported by Safari
https://caniuse.com/link-icon-svg