Google Chrome, the most popular browser to date, introduced dark mode almost 3 years ago. Firefox and all other major browsers now support it as well. On the contrary, even large companies (such as Asics, Sentry, and Under Armour) still use a favicon that is almost invisible on a dark background.
This is especially painful to see for a front-end person with a small tab-hoarding problem (and I think I'm not alone here). Here's what you can do to solve it:
Option 1: Quick Fix - Favicon with Background
If you only have 5 minutes to optimize for dark and light mode, you can simply update your current favicon with a white or black background.
This way your favicon will always look professional regardless of light or dark mode. Google also chose this option and added a white circle around the “G” to create more contrast. However, if you want to have a website with dedicated favicons for dark and light mode, go for the next option.
Option 2 (recommended): SVG-Icon for Light and Dark Mode
For a while now it's been possible to use SVG graphics as favicons. This is convenient: They can be scaled without quality loss, plus you can apply CSS to change the color. The latter we are going to use for our dark mode and light mode favicons.
First open up your SVG file in your preferred editor, in my case that's VS-Code. All you have to do is add internal CSS to your SVG through style tags. Within the tags, you select the fragment(s) of your icon and choose the color for the light theme.
Finally, use the media query to detect dark mode and set your favicon color accordingly
@media (prefers-color-scheme: dark) { ... }
The final SVG for your favicon will look like this:
Favicon Fallback for SVG Icons
The “Can I use” website states 75% support for SVG icons. We can start from here. Nevertheless, for a crucial branding element like the favicon we need 100% browser support.
Therefore you can upload your SVG file to realfavicongenerator.net to create all possible favicon variants and then place the files in the root directory of your website. The generator also creates a code snippet to link all favicon variants in your header.
Note: The link to your previously created SVG favicon should come first, and then the fallback favicons from the generator. The favicon.ico shouldn't be referenced.
How Safari Handles Favicons
Safari takes a different way in handling favicons in dark and light mode. SVG-Icons are unfortunately not supported, therefore it's not possible to change the color through the CSS media query when visitors use dark mode. Safari solves the dark mode difficulty by automatically adding a white or black background if there is a low-contrast favicon.
Example WordPress: How to Add Dark Mode / Light Mode Favicon
Use a WordPress Plugin: SVG-Favicon
It is possible to add the SVG-Favicon through a plugin called SVG-Favicon if you don't know how to edit your child theme. You still have to update your SVG graphic as described previously and then upload it to your WordPress website through the plugin.
Add SVG-Favicon to Your Child Theme Manually
- Add all favicon variants to your root directory via FTP: In my case those are the favicons from the generator
- Update your
functions.php
to link all your favicon variants open your functions.php of your child theme and add the following function
- Final Step: Remove your current favicon from WordPress: If you have already linked your favicon in WordPress you have to delete it so it does not overwrite the ones we just added. To do so go to Appearance → Customize → Site Identity (left column) → Site Icon, and click “Remove”
Different Favicon for Admin Dashboard and Login Screen
In WordPress, the html header for the admin dashboard and login is different from the html header which will be sent to your website visitors. This means the fallback favicon in your root directory will be displayed when you login to your website or are in the admin dashboard.
For me, that's more of a feature than a bug. My fallback favicon is one with an additional white background. Whenever I have two tabs up, one for the admin dashboard and one for the actual website visitors see, I can automatically distinguish those two.
You can add the following two lines to your functions.php to display a different (or the same) favicon on your login and admin dashboard:
Happy coding in dark mode! 🌙