The technique
SVG files can contain a <style> block, and browsers evaluate prefers-color-scheme inside it. Give the shapes a class and change their color in a media query:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
<style>
.mark { fill: #0c1a14; }
@media (prefers-color-scheme: dark) {
.mark { fill: #e6f4ec; }
}
</style>
<path class="mark" d="M6 4h20v6H13v4h11v6H13v8H6z"/>
</svg>Save that as favicon.svg and link it:
<link rel="icon" href="/favicon.ico" sizes="32x32">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">Things to know
- It follows the operating system or browser theme, not your site's toggle. The media query inside the favicon can't see a dark mode class on your page.
- Chrome, Edge, and Firefox support SVG favicons and the media query. Safari supports SVG favicons too. Test it in each browser you care about, because tab bar colors vary.
- Keep the ICO as a fallback. Browsers that can't use the SVG fall back to
favicon.ico, which has one fixed color, so pick colors for it that read on both light and dark. - Use outlines, not text. A
<text>element depends on fonts installed on the visitor's machine. Convert letters to paths.
Often the simpler fix: a filled shape
If you don't want two color schemes, put your mark on a solid background shape (a rounded square or circle) in your brand color. A filled shape is visible on both light and dark tab bars without any media query. That's why most favicons you see are built that way. In FavMint you can preview your icon in light and dark tab mockups side by side to check.