Skip to content

Show different images depending on the theme #11

Answered by BetaHuhn
ilyasabdut asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @ilyasabdut, thanks for reaching out! There are multiple ways to achieve this, it depends on if you are using an <img> tag or are referencing the image via CSS.

With CSS

The easiest way would be to specify two different image URLs as variables in CSS and then use it as the background for a div element:

HTML:

<div id="image-div"></div>

CSS:

/* Light Image */
[data-theme="light"] {
    --image: url("/path/to/light.png");
}

/* Dark Colors */
[data-theme="dark"] {
     --image: url("/path/to/dark.png");
}

#image-div {
   background: var(--image);
}

With JS

If you are using an img tag, you have to use JavaScript to change the src attribute of the img tag. You can do this by listening to …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by BetaHuhn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants