PricingBlog

svg favicon in dark mode not working

  • patrickmast-1280107055343997009

    Patrick Mast

    1 year ago

    I have an SVG icon that includes CSS to ensure the icon is drawn in white when the browser is in dark mode. However, I can't get it to work. Here is the demo URL to test: https://start-test_svg_icon_in_dark_mode.toddle.site/
    1280107055629336647-Pencil_Square_Icon_Black-white.svg
  • patrickmast-1284198027283664907

    Patrick Mast

    1 year ago

    Team toddle. Can someone look at this one please? 🙏 🤩
  • erikbeus-1284223825298329682

    Erik Beuschau

    1 year ago

    Have to added the svg on the page in the demo or only as the favicon? I can only see a link to the svg on your example page
  • patrickmast-1284433488485023785

    Patrick Mast

    1 year ago

    Hey @Erik Beuschau, thank you for your attention on this issue. I have added the SVG file also as a "Media" file in the settings for the project. Than I took the "Full" link to that and used it as the favicon. As you can see in the source code of the page.
    1284433488279506944-image.png
  • erikbeus-1285145448734130226

    Erik Beuschau

    1 year ago

    The favicon of your project appears to have been removed now? 🤔
    Anyway, I just tried the following in Chrome:
    - Opening the svg directly from our cdn https://nordcraft.com/cdn-cgi/imagedelivery/ZIty0Vhmkm0nD-fBKJrTZQ/test_svg_icon_in_dark_mode:JcvRzt6i.svg/32 - switching my browser between light/dark mode doesn't affect the color of the svg
    - Downloading the file and opening it in Chdome - switching my browser between light/dark mode does affect the color of the svg 🤔
    Do you experience the same?
  • patrickmast-1285153083395145831

    Patrick Mast

    1 year ago

    @Erik Beuschau Correct! 😉
  • patrickmast-1285975965612376117

    Patrick Mast

    1 year ago

    Hey @Erik Beuschau, let me know when I can test. Thanks! 😎
  • erikbeus-1286018414162481257

    Erik Beuschau

    1 year ago

    Sorry, I've been occupied with other things the past few days. I'll investigate what might cause the difference I noticed above tomorrow and let you know what I find out 🤞
  • erikbeus-1286020436114804799

    Erik Beuschau

    1 year ago

    Actually, I think I might have a solution:
    - From the findings above, it seemed that svg files hosted on Cloudflare (/cdn-cgi/...) did not respect the browser's theme
    - I tried uploading the file to GitHub (see file here https://raw.githubusercontent.com/erik-beus/test/refs/heads/main/icon.svg)
    - I used the url above for my project icon
    - Now, when I visit my app, the icon is black in light mode and white in dark mode - see app here https://start-gray_leia_organa_abundant_loon.toddle.site/ (it requires a refresh after switching the theme in Chrome)

    I'll check if we can safely upload svg images to Cloudflare, but perhaps the above could be a decent work-around for now 👍
    1286020436177584179-Screenshot_2024-09-18_at_19.44.33.png
  • ebud-1286022823625887875

    ebud

    1 year ago

    @Patrick Mast if SVGs are being annoying you can do something like this:

    <head>
    <link id="favicon" rel="icon" href="favicon-light.png" type="image/png">
    <script>
    function updateFavicon() {
    const favicon = document.getElementById("favicon");

    if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
    favicon.href = 'favicon-dark.png';
    } else {
    favicon.href = 'favicon-light.png';
    }
    }

    updateFavicon();

    window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateFavicon);
    </script>
    </head>


    Read an article a while ago about someone making a whole video game in a favicon :]