Skip to content

Instantly share code, notes, and snippets.

@SilasRodrigues19
Last active January 20, 2024 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SilasRodrigues19/ce881b5ca46eb0c93cafc103b0272e80 to your computer and use it in GitHub Desktop.
Save SilasRodrigues19/ce881b5ca46eb0c93cafc103b0272e80 to your computer and use it in GitHub Desktop.
Invert the favicon color according to the browser theme, to prevent the white logo from being applied to a white background or the black logo from being applied to a black background
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
  <path fill="#000"
    d="m21.484 7.125l-9.022-5a1.003 1.003 0 0 0-.968 0l-8.978 4.96a1 1 0 0 0-.003 1.748l9.022 5.04a.995.995 0 0 0 .973.001l8.978-5a1 1 0 0 0-.002-1.749" />
  <path fill="currentColor"
    d="m12 15.856l-8.515-4.73l-.971 1.748l9 5a1 1 0 0 0 .971 0l9-5l-.971-1.748z" />
  <path fill="currentColor"
    d="m12 19.856l-8.515-4.73l-.971 1.748l9 5a1 1 0 0 0 .971 0l9-5l-.971-1.748z" />

    <style>
      @media (prefers-color-scheme:dark){:root{filter:invert(100%)}}
    </style>

</svg>

Explanation:

  1. The <style> element is used to embed the CSS code within the SVG file.
  2. @media (prefers-color-scheme: dark): This media query checks if the user's device prefers a dark color scheme.
  3. :root: Refers to the highest-level element in the document.
  4. filter: invert(100%): Inverts the colors of the SVG element.

Keep in mind that the filter: invert(100%) will only affect the colors within the SVG, as the filter is applied to the SVG element itself. The rest of the HTML or document content outside the SVG will not be affected by this specific CSS rule.

SVG Example:

<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
  <path fill="#000"
    d="m21.484 7.125l-9.022-5a1.003 1.003 0 0 0-.968 0l-8.978 4.96a1 1 0 0 0-.003 1.748l9.022 5.04a.995.995 0 0 0 .973.001l8.978-5a1 1 0 0 0-.002-1.749" />
  <path fill="currentColor"
    d="m12 15.856l-8.515-4.73l-.971 1.748l9 5a1 1 0 0 0 .971 0l9-5l-.971-1.748z" />
  <path fill="currentColor"
    d="m12 19.856l-8.515-4.73l-.971 1.748l9 5a1 1 0 0 0 .971 0l9-5l-.971-1.748z" />
    <style>
      @media (prefers-color-scheme:dark){:root{filter:invert(100%)}}
    </style>
</svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment