DEV Community

Cover image for Using Dark/Light Mode Specific Images in GitHub Markdown
Arnav Kumar
Arnav Kumar

Posted on • Edited on

Using Dark/Light Mode Specific Images in GitHub Markdown

Hey There Devs! ๐Ÿ‘‹

Hope you are doing great with your projects.

TOC

Why this post? ๐Ÿค”

As a developer we develop projects, write code all day around and we might also publish it on GitHub to let others understand and use our project.

And Markdowns play a vital role in explaining the code, branding your projects, showcasing your stats and so on.

And you all know how much is the craze of the Dark Mode nowadays especially between the Devs. But most browsers have light mode as their default.

So, here comes the actual point of this post. By default GitHub adapts most of the parts of the Markdown according to the theme. But it doesn't do anything with your images by default.

Imagine an image that was made to look good on white background with black as its foreground color. it will not even be notable in the dark mode.

So, you may want to optimize your Images according to the current theme of the viewer's browser. And also maybe to look a bit cool and professional ๐Ÿ˜….

Procedure ๐Ÿงช

The procedure is very simple. No I am not joking its actually very simple.

  1. Do Create 2 separate versions of your images one optimized for light mode and one for dark mode. For example take this image of Termux. I have created two variants of the same image, one for light and one for dark:
    Light Mode Variant
    Light Mode Variant
    Dark Mode variant
    Dark Mode Variant
  2. Now, here comes the magical thing ๐Ÿช„. To make an Image only visible in Dark Mode, add #gh-dark-mode-only at the end of the image url. And to make an Image Only Visible in Light Mode, add #gh-light-mode-only.

Example:



![Termux Logo](https://user-images.githubusercontent.com/72879799/153904003-d7dee710-6552-4d23-a803-7a9a0ba67d92.png#gh-dark-mode-only)
![Termux Logo](https://user-images.githubusercontent.com/72879799/153904095-9d78a019-8495-4035-8174-e3da8e4dd66b.png#gh-light-mode-only)


Enter fullscreen mode Exit fullscreen mode

Now the Dark Mode variant of the Image will only be visible in the Dark mode and the Light Mode variant will only be visible in the Light Mode.

The Image URLs above are just for demonstration. Replace them with your own image URLs and you are Ready to Go!

And it works ๐Ÿฅณ๐ŸŽ‰!
Image description

Live Example โšก:

You can see the working example at my GitHub.

Disclaimers โš ๏ธ

Note that it will not adapt according to the Theme if the image is not hosted on the GitHub

Latest comments (40)

Collapse
 
sscsps profile image
Sahil Soni

Just used this and found that when you have the other theme than the one provided, the element is still displayed, and there's an extra (empty) line where the expected image is to be,

to Mitigate this, I just added both images in one line, take a look at this for reference github.com/SscSPs/color/issues/47

Collapse
 
arnavkr profile image
Arnav Kumar

I already mentioned those in disclaimers thought can paste your images in the markdown editor and it will get uploaded then you can use it the way you want

Collapse
 
aymhenry profile image
Ayman Henry • Edited

I use app to do work for me.
I use (typo) app.

Collapse
 
scalaadeveloper profile image
Scalaadeveloper

Do you have a video for this because I can't understand your text I can't speak english

Collapse
 
arnavkr profile image
Arnav Kumar • Edited

Created a simple demo, hope you get it now

Collapse
 
scalaadeveloper profile image
Scalaadeveloper

Thank you very much

Collapse
 
incrementis profile image
Akin C. • Edited

Hello Arnav Kumar,

thank you for your article.
I enjoyed reading it and testing what you wrote.
I took two images that let the user know what mode he/she is in.
This is interesting because I'm telling the user what mode he/she is in without me knowing what mode the user is really in :D!

Collapse
 
arnavkr profile image
Arnav Kumar

What a creative idea lol! โค๏ธ๐Ÿ‘

Collapse
 
virtualvivek profile image
Vivek Verma

This is interesting and much helpful for my projects.

Collapse
 
arnavkr profile image
Arnav Kumar

Thanks! โค๏ธ

Collapse
 
melfordd profile image
Melford Birakor

Thank you

Collapse
 
arnavkr profile image
Arnav Kumar

โค๏ธ

Collapse
 
lionelrowe profile image
lionel-rowe

Really not a fan of GitHub's approach here. Markdown is supposed to be a format that's compatible with many platforms, and a valid URL can only have one hash. That means that other platforms either have to implement the URL hash matching logic along with the gh-* names, or they have to implement it in an inconsistent way, such as through query params (https://example.com/img.jpg?gitlab-theme=dark&discourse-theme=dark&some-other-platform=dark#gh-dark-mode-only). Why not just use a single, cross-platform query param (?theme=dark), add this as a documented part of GitHub-flavored markdown, and be done with it? Other platforms that fully or partially support GFM can then add support in their own time, without using up the single "slot" in the URL's hash or using explicitly gh-* named parameters.

Collapse
 
arnavkr profile image
Arnav Kumar

I am totally agree with your points here. And also i didn't like how they restricted to the images that are only hosted via github. Ig they did that to overcome that hash issue in urls. They might have thought that if they won't allow any other urls, there won't be any problems in using hash.
tbh its a very bad and untidy approach to simple thing. In my views either the query paramaters are good or specifying them as options like ![Dark](https://example.com/dark_img.png){theme = dark}.

Seems like its time to open an issue at the their support or in the github repo

Collapse
 
arnavkr profile image
Arnav Kumar

In the community the actual guy who requested for the feature actually had suggested somewhat better approach but the GitHub doesn't seem to take his suggestions.

Image

Collapse
 
lionelrowe profile image
lionel-rowe

I guess they are owned by Microsoft now so maybe we shouldn't be surprised by this kind of embrace-extend-extinguish bullshit.

Collapse
 
nicolus profile image
Nicolus

To be fair github started using "github flavored markdown" (which basically means that if you clone a repository on any other host your readme.md will not display correctly) loooong before Microsoft every thought about buying them.

Thread Thread
 
lionelrowe profile image
lionel-rowe

I'm on board with GFM, especially now it has become a de-facto standard (albeit one among several). Ideally there'd be a proper spec for it, but there isn't one of those even for the original version of Markdown.

The problem with gh-* hash params is that it forces other implementers of GFM to implement this explicitly GitHub-centric API that even end users have to know about. I mean maybe that's intentional from a "branding" point of view, but it's incredibly obnoxious from a design point of view.

Collapse
 
arnavkr profile image
Arnav Kumar

Microsoft keep doing unusual things ๐Ÿ˜‘

Collapse
 
kiril6 profile image
Kiril Delovski

short but enough and super cool. :)

Collapse
 
arnavkr profile image
Arnav Kumar

Thanks!โค๏ธ

Collapse
 
afzl210 profile image
Mohd Afzal Khan

Thanks

Collapse
 
arnavkr profile image
Arnav Kumar

โค๏ธ

Some comments have been hidden by the post's author - find out more