🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Performance••By Komal Nakrani

Astro Favicon in Dark Mode: An SVG That Adapts

Make your Astro favicon follow dark mode: an SVG with a prefers-color-scheme media query inside, saved in public/, linked from the layout, with a PNG fallback.

#Astro#Favicon#Dark Mode#SVG

To make an Astro favicon follow dark mode, use an SVG favicon with a <style> block inside it that changes the fill under @media (prefers-color-scheme: dark). Save it as public/favicon.svg, link it with type="image/svg+xml", and supporting browsers switch the icon whenever the system theme changes. No JavaScript and no second file are needed.

Key Takeaways

  • SVG favicons can contain CSS, including media queries.
  • prefers-color-scheme inside the SVG follows the operating system or browser theme, not your site’s toggle.
  • Chromium browsers and Firefox support SVG favicons; Safari falls back to other icons.
  • Keep a favicon.ico or PNG fallback alongside it.

The SVG

<!-- public/favicon.svg -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  <style>
    .mark { fill: #0f172a; }
    @media (prefers-color-scheme: dark) {
      .mark { fill: #f8fafc; }
    }
  </style>
  <path class="mark" d="M6 4h8l12 24h-8L6 4z" />
  <circle class="mark" cx="24" cy="8" r="4" />
</svg>

Use classes on the shapes and define colors in the <style> block. Inline fill attributes would override the media query.

The media query follows the browser or system theme, not your page. A dark browser theme shows the light mark, which keeps a dark logo visible on a dark tab.

---
// src/layouts/Layout.astro
---
<head>
  <link rel="icon" href="/favicon.ico" sizes="32x32" />
  <link rel="icon" href="/favicon.svg" type="image/svg+xml" />
  <link rel="apple-touch-icon" href="/apple-touch-icon.png" />
</head>

Browsers without SVG favicon support use the ICO. The sizes attribute on the ICO keeps Chromium from choosing it over the SVG. Our add a favicon tip covers the full file set.

A badge that works on both

If one icon must work on both light and dark tabs, give it its own background so it never depends on the tab color:

<!-- public/favicon.svg -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  <rect width="32" height="32" rx="7" fill="#0f766e" />
  <path d="M10 8h5l7 16h-5L10 8z" fill="#ffffff" />
</svg>

This also suits search results, where Google shows the favicon on a light or dark page depending on the user.

Following your site toggle instead

The SVG cannot see your site’s .dark class, because the browser renders favicons separately from the page. To match a manual toggle, swap the link’s href from the same script that sets the theme:

<script is:inline>
  const dark = document.documentElement.classList.contains('dark');
  document.querySelector('link[type="image/svg+xml"]')?.setAttribute('href', dark ? '/favicon-dark.svg' : '/favicon.svg');
</script>

Most sites do not need this; following the system theme is what users expect from browser chrome. Pair it with our Tailwind dark mode tip if you do.

Testing

In Chrome DevTools, open Rendering and set “Emulate CSS media feature prefers-color-scheme” to dark; the favicon updates in the tab. Browsers cache favicons hard, so reload in a private window after edits. MDN’s prefers-color-scheme reference covers the media feature. The project structure docs explain how public/ is served, and our Astro themes all ship an SVG favicon you can edit this way.

FAQ

Can a favicon change with dark mode?

Yes, if it is an SVG. Put a prefers-color-scheme: dark media query in a <style> block inside the SVG and change the fill colors there.

Does Safari support dark mode SVG favicons?

Safari does not use SVG favicons for tabs, so it shows your ICO or PNG fallback. Keep a fallback with good contrast on both backgrounds.

Why does my SVG favicon not switch colors?

Check that colors are set in the <style> block via classes, not as inline fill attributes, and test with a private window because the old icon is probably cached.