Your brand navy is #0b1f3a. It looks sharp on a white tab strip. Then a user switches their machine to dark mode, Chrome’s tab bar goes to roughly #202124, and your icon disappears into it: run the WCAG relative-luminance formula on those two colours and the contrast comes out at about 1.03 to 1. That is not “low contrast”. That is the same colour twice.
The fix people usually reach for is a lighter brand colour, which compromises the light-mode version, or two files swapped by JavaScript, which works and is more machinery than the problem deserves.
There is a better option. An SVG favicon dark mode setup needs exactly one file, because an SVG can carry its own <style> block, and that stylesheet can contain a @media (prefers-color-scheme: dark) rule. The browser evaluates it and flips your fills. No script, no second request, no flash of the wrong colour.
The technique is short enough to explain in a paragraph and has enough sharp edges to justify the rest of this article. The sharpest one: the media query does not follow your page’s theme. It follows the browser’s.
The short answer
Put a <style> element inside your SVG favicon with a @media (prefers-color-scheme: dark) block that changes the fill colours, then declare it in the head with type="image/svg+xml" alongside an ICO fallback:
<link rel="icon" href="/favicon.ico" sizes="32x32">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
Chrome, Edge and Firefox will render the SVG and honour the media query. Safari’s SVG favicon support has been inconsistent between versions, so the ICO line is not optional. Order matters: put the SVG last, because browsers pick the last declaration they can actually use, and the type attribute lets them skip formats they do not support without downloading them first.
The query resolves against the browser’s colour scheme, not your page’s. If a user runs their OS in light mode on a site with a forced dark theme, the favicon stays in its light-mode colours. That is correct behaviour, and it surprises everyone the first time.
The complete file, ready to paste
This is a working favicon. Save it as icon.svg, put it at your site root, and it will flip between a navy square with a white mark and a white square with a navy mark.
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
<style>
.bg { fill: #0b1f3a; }
.fg { fill: #ffffff; }
@media (prefers-color-scheme: dark) {
.bg { fill: #ffffff; }
.fg { fill: #0b1f3a; }
}
</style>
<rect class="bg" width="64" height="64" rx="12"/>
<path class="fg" d="M20 46V18h7.6l8.9 15.6L45.4 18H53v28h-6.6V29.7l-7.1 12.2h-5.2l-7.1-12.2V46z"/>
</svg>
That file is 402 bytes on disk and 286 bytes over the wire once your server gzips it. A 32 pixel PNG of the same mark is typically several times that, and the SVG stays crisp at every size a browser might ask for.
Four details in there are doing real work. The xmlns attribute is required; without it the file is not valid SVG and browsers will refuse it. The viewBox gives the renderer a coordinate system to scale from, which matters because there is no fixed size for a favicon. The classes exist so the media query has something to target. And the <rect> fills the whole canvas, so the icon reads as a solid tile rather than a floating shape, which is what makes it survive on both backgrounds.
Swap the path for your own mark. If your logo only exists as a PNG, PNG to SVG will trace it into paths you can drop straight in, and Color Picker will give you the hex values if you are eyedropping from a brand sheet.
Why the media query follows the browser, not your page
Here is the part that trips people, and it took me a while to accept it as correct rather than a bug.
When a browser renders an SVG as an image, it renders it in an isolated context. Your page’s CSS does not reach it. Your page’s color-scheme declaration does not reach it. The prefers-color-scheme query inside the file is evaluated against the user agent’s own colour scheme, which usually tracks the operating system setting.
Think about what the icon is actually sitting on. It sits in the tab strip, which is browser chrome, painted by the browser according to the OS appearance. If the favicon followed your page theme, a light icon would appear on a light tab strip whenever a user with light OS settings visited your dark-themed site. Following the chrome is the only rule that produces the right result in every combination.
The practical consequence: a site with its own theme toggle will not see the favicon change when a user clicks it. Only the OS setting moves the icon. I have seen this filed as a bug more than once, and it is not one.
If you want your page’s theme toggle to affect browser UI, that is a different tag entirely:
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#ffffff">
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#0b1f3a">
That controls the address bar tint on Android Chrome and the toolbar tint in some Safari versions. It has nothing to do with the favicon, but it belongs in the same block of head markup and people conflate the two constantly.
Declaring it correctly, and why order matters
Browsers walk your icon declarations and select one. The general behaviour, consistent enough across Chromium and Firefox to plan around, is that the last declaration a browser understands wins. The type attribute lets a browser rule out a format it cannot render without fetching the file first.
So this is right:
<link rel="icon" href="/favicon.ico" sizes="32x32">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="manifest" href="/site.webmanifest">
And this is subtly wrong, because a browser with SVG support may take the ICO:
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="icon" href="/favicon.ico" sizes="32x32">
Notice that the ICO line has no type attribute. That is deliberate. Historically, some browsers have handled a missing type more forgivingly than a type="image/x-icon" value that does not match what the server sends, and since ICO is the fallback you cannot afford to lose, leaving it untyped is the safer bet.
Your server also has to send the right MIME type. image/svg+xml for the SVG, image/x-icon or image/vnd.microsoft.icon for the ICO. A server that hands back application/octet-stream will produce a blank tab and no console error, which is one of the nine causes in the favicon debugging walkthrough.
Browser support, honestly
I am going to give you a table and then immediately tell you not to trust it as permanent.
| Browser | Renders SVG favicon | Honours prefers-color-scheme inside it | Notes |
|---|---|---|---|
| Chrome | Yes, since version 80 | Yes | The reference implementation for this technique |
| Edge | Yes, Chromium versions | Yes | Same engine, same behaviour |
| Firefox | Yes, for many versions | Yes | Support predates Chrome’s |
| Safari | Inconsistent between versions | Not reliably | Keep the ICO fallback |
| Chrome on Android | Yes | Yes | Tab icons follow OS scheme |
| Safari on iOS | Inconsistent | Not reliably | Home screen uses apple-touch-icon regardless |
Safari is the reason this table exists at all. Its behaviour with rel="icon" pointing at an SVG has changed across releases, and the correct engineering response is not to memorise which version does what. It is to always declare a raster fallback and stop thinking about it.
Verify anything you are about to depend on against caniuse or MDN at the time you build. Support tables in blog posts, including this one, describe the day they were written. This particular corner of the platform has moved more than once and there is no reason to assume it has stopped.
What will not work inside a favicon SVG
An SVG used as an image runs in a restricted mode. The restrictions are there for good security reasons and they are absolute.
No scripting. A <script> element in the file is ignored entirely, so you cannot compute colours at render time or read anything about the environment.
External resources are out as well. That means no <link> to an external stylesheet, no @import, no <image href="photo.png"> pointing at another file, and no web fonts. Everything must be in the one file. If your mark uses a custom typeface, convert the text to paths before you export, because <text> will fall back to whatever the rendering environment considers a default font and you will not like the result.
Animation is a maybe, and I would treat it as a no. Browsers render SVG-as-image in a mode that permits declarative animation, but favicon rendering in practice tends to be a static snapshot, and Chrome does not animate tab icons. Build for a still frame.
Data URIs for embedded raster content technically work, but they defeat the point. If you are base64-encoding a PNG inside an SVG to use as a favicon, ship the PNG.
Filters, gradients and masks are supported by the renderer, but see the next section for why you probably should not use them.
Designing something that survives at 16 pixels
An SVG favicon is vector, which people take to mean detail is free. It is not. At the size a tab icon actually renders, roughly 16 CSS pixels, you have a 16 by 16 grid to say something in. Vector precision does not add pixels; it just means the mush is computed accurately.
Test at the real size. Not zoomed to 400 percent in your editor, where everything looks fine. Render at 16 and 32 and look at it in a browser tab next to your other open tabs.
The elements that die first are thin strokes below about two device pixels at the target size, serifs on lettering, gradients that flatten into a single muddy value, drop shadows that read as a smudge, and any outline thinner than the shape it outlines. Counters inside letters close up. Two similar shapes become one blob.
Reduction usually goes in one direction: full logo, then logomark, then a single letter. Most brands should land on the logomark or the letter. There is more on that process in designing a favicon that reads at 16 pixels, including where to stop cutting.
One counterintuitive point on padding: a favicon with almost no padding reads larger and usually better than one that respects your brand’s clear-space rules. Clear space exists for print and for logos next to other content. A tab icon is a lone shape in a slot. Fill the slot.
Safari pinned tabs and the mask-icon tag
Separate feature, often confused with this one. When a macOS Safari user pins a tab, Safari can show a monochrome silhouette instead of your favicon, declared like this:
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#0b1f3a">
The requirements are strict and unusual. The SVG must be a single-colour silhouette, drawn in 100 percent black, on a transparent background, with no other fills. Apple’s guidance has specified a square viewBox for it. The color attribute is what tints the black silhouette, and Safari overrides it when the tab is active.
This is a small, macOS-only, legacy-flavoured feature. Including it costs one line and one small file. Skipping it costs you a grey silhouette for the subset of Safari users who pin tabs. I include it and do not think about it again, but I would not spend an afternoon on it either.
If you would rather not hand-author four separate icon files, the Favicon Generator will take one square source image and produce the SVG, the multi-resolution ICO, the apple-touch-icon and the manifest PNGs together, with the head block written out for you to copy. It runs entirely in your browser, so an unreleased brand mark never touches a server, which is the reason we built it that way rather than as an upload-and-wait service.
Testing it without fooling yourself
This is the part where most people declare victory too early, because DevTools lies to you here in a specific and understandable way.
- Serve the file and confirm the header. Run
curl -sI https://example.com/icon.svg | grep -i content-typeand check you getimage/svg+xml. Anything else and nothing downstream matters. - Open the SVG URL directly in a browser tab. It renders as a standalone document, and the media query works in that context too, so you get an immediate read on whether the CSS is valid.
- Switch your operating system appearance. macOS: System Settings, Appearance. Windows 11: Settings, Personalization, Colors, “Choose your default app mode”. This is the real test.
- Reload the page, or better, open it in a new tab. Some browsers repaint the icon on scheme change without a reload and some do not.
- Do not rely on the DevTools Rendering panel’s “Emulate CSS media feature prefers-color-scheme”. It changes the emulated scheme for the page. The favicon is rendered against the browser’s own scheme, so the emulation frequently will not flip it. Seeing no change there does not mean your file is broken.
- Check the fallback path by loading the site in Safari and confirming you get the ICO rather than nothing.
- Clear the favicon cache between attempts. Chrome keeps icons in a separate store that a hard reload does not clear, so add
?v=2to thehrefwhile you are iterating.
The problems you will hit
Say Dana runs a developer docs site. Brand navy #0b1f3a, white logotype, everything approved by a designer who only ever looked at it on a light background.
Against a white tab strip the navy mark scores about 16.5 to 1. Excellent. Against Chrome’s dark tab strip at roughly #202124 it scores 1.03 to 1, which is why her icon looks like an empty slot to every dark mode user. Even against the slightly lighter inactive tab shade around #35363a it only reaches about 1.37 to 1. Still invisible.
She has three options. Lighten the brand navy, which the designer will reject and which weakens the light-mode version. Add a permanent white background tile, which works in both modes and costs her the transparent, floating look. Or ship the SVG above, which keeps navy-on-white in light mode and inverts to white-on-navy in dark mode, scoring about 16.1 to 1 against the dark strip.
In the end she ships the SVG. Then three things go wrong in the first hour.
First, nothing changes when she clicks her site’s own theme toggle. Correct behaviour, covered above. Her OS is still in light mode.
Then Safari: the icon works in Chrome and is blank there. She has declared only the SVG. Adding the ICO line above it fixes it, and she builds that ICO properly with PNG to ICO rather than exporting a screenshot of the SVG at 32 pixels, because an ICO should carry 16, 32 and 48 pixel frames in one container and a single-frame file downscales badly.
Last, it works everywhere except her own main Chrome window, which still shows the old icon. Favicon cache. She opens the SVG URL directly, reloads, and it appears.
The failure I would watch for beyond those is a build step that minifies SVGs and strips what it considers dead CSS. Some optimisers will remove a <style> block whose selectors it cannot match statically, or inline the class fills as presentation attributes and drop the media query with them. If your icon flips in development and stops flipping in production, look at your asset pipeline before you look at the browser.
The set I would actually ship
Four files, four lines of head markup. The SVG for modern browsers and for the dark mode flip. A multi-resolution favicon.ico at the root, holding 16, 32 and 48 pixel frames, because it is the only icon a browser will fetch with no tag at all. A 180 pixel apple-touch-icon.png for iOS home screens. And a manifest with 192 and 512 pixel PNGs if you want the site installable.
That is the whole argument of what to actually ship in 2026, and the reason to keep the set small is that every extra file is another thing that can go stale during a rebrand. Twenty-file bundles were a reasonable answer to a browser situation that no longer exists.
If you want the four files without hand-editing any of them, the icon generator outputs exactly that set from a single square upload, including the dark mode SVG with the media query already wired up. Change one hex value later and you regenerate in seconds.
Frequently asked questions
How does an SVG favicon know the user is in dark mode?
The SVG carries its own <style> block containing a prefers-color-scheme media query. The browser renders the file in its own isolated context and evaluates that query against the browser and operating system colour scheme, not against your page. So the icon follows the tab strip it is sitting on, which is the behaviour you want in every combination of page theme and system theme.
Does Safari support SVG favicons?
Safari’s handling has been inconsistent across versions and it is the main reason a fallback is still required. Declare an ICO or PNG alongside the SVG and Safari will use that if it cannot render the vector. Check caniuse or MDN for the current state before you rely on SVG alone, because this specific behaviour has moved more than once and a table in an article describes the day it was written.
Why does my favicon media query work on the page but not in the tab?
You are probably testing with DevTools colour scheme emulation, which changes the rendering context for the page rather than for the browser chrome. Favicons are rendered against the browser’s own scheme, so the emulation frequently will not flip them. Switch your operating system appearance setting instead, then reload the page, and bust the favicon cache with a version query string while you iterate.
Can I use JavaScript or an external stylesheet in a favicon SVG?
No. Browsers render an SVG used as an image in a restricted mode with no scripting and no external resource loading. External CSS files, @import rules, web fonts, referenced raster images and <script> elements are all ignored. Everything has to be inline in the single file, and any text should be converted to paths so it does not depend on a font that will not load.
What size should an SVG favicon be?
There is no fixed pixel size, which is the point of using vector. Set a square viewBox such as 0 0 64 64 and design as if it will render at 16 CSS pixels, because that is the common tab size. Vector precision does not save fine detail at that scale. Keep the shape count low, the strokes thick, and the colour contrast high against both a light and a dark strip.
Do I still need favicon.ico if I have an SVG?
In practice yes. A root favicon.ico is the only icon a browser will fetch with no HTML tag at all, which covers requests made by convention, some feed readers and older tooling that never learned about rel="icon". It costs a few kilobytes, it is the fallback Safari may need, and it removes a whole category of support question. Keep it.
Ship it and move on
The technique is genuinely small. One file, one media query, two extra lines of CSS, and an icon that stops disappearing for the substantial share of your users who run their machines dark.
What I would hold onto beyond the code: the media query follows the browser, not your page, and no amount of testing your own theme toggle will show you the behaviour. Change your OS appearance. And keep the ICO fallback declared first, because Safari’s position on SVG favicons has not been stable and there is no version of this where you win by being clever about it.
Build the four-file set once, version the href, and the next time someone changes the brand colour you will be editing two hex values in a 402 byte file.