Change images on scheme toggle #2744
-
I want to change the images when toggled on schemes. Basically would want dark-themed images for dark mode and light-themed images for light mode. I have both images as assets. What would be the appropriate way to achieve the same? .md file
js file
But I am not getting any callbacks when scheme is changed from the nav bar. I have tried adding Can someone help? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
We could use some help with this. We didn't know if this was the right place to ask. But if not, kindly point us to the right place. The functionality we're looking for, is similar to what's been there in the homepage of Simple Analytics |
Beta Was this translation helpful? Give feedback.
-

 Alternatively, use The first image will load if the system prefers a light color scheme, the second will load if it prefers dark, and the first will load as a fallback. <picture>
<source media="(prefers-color-scheme: light)" srcset="https://unsplash.com/photos/JHyiw_dpALk/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8Mnx8c3VufGVufDB8fHx8MTY1MTQ2NTg3Mg&w=110">
<source media="(prefers-color-scheme: dark)" srcset="https://unsplash.com/photos/pd4lo70LdbI/download?ixid=MnwxMjA3fDB8MXxhbGx8NjV8fHx8fHwyfHwxNjUxNDY4MjQy&w=110">
<img src="https://unsplash.com/photos/JHyiw_dpALk/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8Mnx8c3VufGVufDB8fHx8MTY1MTQ2NTg3Mg&w=110">
</picture> I don't know if the below embedded demo will load, but we'll see… For SVG images, you can embed CSS that uses media queries into the |
Beta Was this translation helpful? Give feedback.
This has been implemented in version 8.1.1 by adding a
#only-light
or#only-dark
hash fragment to the image URL.Alternatively, use
prefers-color-scheme
CSS media queries—instead of using the toggle switch—to enable automatic dark/light switching for images or SVG files. This has the added benefit of being framework-agnostic.The first image will load if the system prefers a light color scheme, the second will load if it prefers dark, and the first will load as a fallback.