Picture and selector based themes (dark mode) #1829
Replies: 3 comments 4 replies
-
Here is idea from remcohaszing/rehype-mermaid#16 (comment). Write rehype plugin to transform from <picture>
<source media="(prefers-color-scheme: dark)" srcset="...">
<img src="...">
</picture> to <img class="dark" ... />
<img class="light" ... /> And then one can add CSS: html[data-theme="light"] .dark {
display: none;
}
html[data-theme="dark"] .light {
display: none;
} UPDStarlight already has classes for that <img class="light:sl-hidden" ... />
<img class="dark:sl-hidden" ... /> |
Beta Was this translation helpful? Give feedback.
-
I see you found our The CSS solution is not 100% ideal because browsers will sometimes initialise downloading the hidden image before they realise it’s hidden. So that can mean a pointless download, but the alternatives (e.g. JS-powered techniques) usually are worse for performance, because the browser may not be able to start preloading an image at all until quite late. So it’s a question of the lesser evil in this case. |
Beta Was this translation helpful? Give feedback.
-
Hello @stereobooster. I came across the same problem. I'm using |
Beta Was this translation helpful? Give feedback.
-
Let's say I have HTML like this
And I want that picture would switch dark/light mode depending on the theme (through
starlight-theme-select
)At first I thought it would be enought to set
color-scheme
. It works for buttons, inputs etc. But not for picture... sighThe only hacky way to do this which I can think of is:
Is there a better way to do this?
Striclty speaking this is not Starlight specific, basically any project that uses selector based theme would have the same issue. I'm surprised that I haven't find any mentions of this issue anywhere else. Do I miss something?
Beta Was this translation helpful? Give feedback.
All reactions