DEV Community

Cover image for Switchable theme based Favicons
Anwesh Gangula
Anwesh Gangula

Posted on • Updated on

Switchable theme based Favicons

GIF showing a demo  of theme based switchable favicons

SVG is by far the best option for any logo specific requirements. And the same applies for favicons too.

Another benefit of using SVG as a favicon is that you can have dynamic favicon that changes color based on users system preference - Dark Mode or Light Mode. And you don't even have to maintain multiple files.

You can use inline CSS within your SVG file to dynamically change the colors while the SVG is being loaded. In fact, you can use this SVG anywhere else in your project and it will still be dynamic without any additional changes.

To test it out, all you need is to save the below SVG (which is just a circle) in your project and add it to your HTML as favicon like so <link rel="icon" href="svgfile.svg" />

<svg viewBox="0 0 350 350" version="1.1" xmlns="http://www.w3.org/2000/svg" width="350" height="350">

    <style>
        circle {
        fill: white;
        }

        @media (prefers-color-scheme: light) {
          circle {
          fill: black;
          }
        }
    </style>

<circle cx="150" cy="150" r="150"  />
</svg>
Enter fullscreen mode Exit fullscreen mode

Add favicon in your HTML code like below

<head>
  <link rel="icon" href="svgfile.svg" />
  ...
</head>
Enter fullscreen mode Exit fullscreen mode

Additional References:

Dark Mode Favicons - CSS Tricks

Top comments (0)