DEV Community

Cover image for One line - Dark Mode using CSS
Akhil Arjun
Akhil Arjun

Posted on

One line - Dark Mode using CSS

This is an absolute no-brainer method of converting an already developed website to support dark mode.

Without further ado let's get into it! 👾

Consider this news application for example


News Peek Light

Now add the magic CSS



html[theme='dark-mode'] {
    filter: invert(1) hue-rotate(180deg);
}


Enter fullscreen mode Exit fullscreen mode

Voila! you are done ✌

Dark mode achieved


News Peek Dark

Explanation

Now let's try and understand what is going on under the hood.

The filter CSS property applies graphical effects like blur or color shift to an element. Filters are commonly used to adjust the rendering of images, backgrounds, and borders. (Reference: MDN Web Docs)

For this dark mode, we would be using two filters namely invert and hue-rotate

invert filter helps to invert the color scheme of the application. So, black becomes white and white becomes black and similarly for all the colors.

hue-rotate filter helps us with all the other colors that are not black and white. Rotating Hue by 180 degrees, we make sure the color theme of the application does not change but just attenuate the colors of it.


comparison


The only catch with this method is, it will also invert all the images in your application. So we will add the same rule to all images to reverse the effect.



html[theme='dark-mode'] img{
    filter: invert(1) hue-rotate(180deg);
}


Enter fullscreen mode Exit fullscreen mode

and we will also add a transition to the HTML element to make sure the transition does not become flashy!



html {
    transition: color 300ms, background-color 300ms;
}


Enter fullscreen mode Exit fullscreen mode



Result:
Dark Mode Example


There we have our Dark mode implemented. Great job guys!

My days are fueled with coffees and only coffees. So I know, you know what we all should do 🤞


Buy Me A Coffee


Well done

Oldest comments (52)

Collapse
 
rossangus profile image
Ross Angus

This is a wonderful post.

Collapse
 
akhilarjun profile image
Akhil Arjun

Thanks mate 👍

Collapse
 
nemo011 profile image
Nemo

Awesome trick! Thanks Akhil. ☺️

Collapse
 
akhilarjun profile image
Akhil Arjun

You are welcome 🕺

Collapse
 
livetvchannels profile image
Trieu.iv

I'm trying
Thanks 👍

Collapse
 
akhilarjun profile image
Akhil Arjun

Do let me know how it goes 🙂
I have used it in a demo application i am. Building for another PWA tutorial
akhilarjun.github.io/news-peek/

Check it out for implementation of the dark mode 👍

Collapse
 
jadercm profile image
Jáder Carvalho de Medeiros

Excellent! Could you please share the JavaScript code to toggle between themes as well?

Collapse
 
akhilarjun profile image
Akhil Arjun

I am writing a post for the javascript side of it too. Will post it today itself 🙂

Collapse
 
akhilarjun profile image
Akhil Arjun
Collapse
 
nickwatton profile image
Nick Watton

This is wonderful. So simple. Thank you

Collapse
 
akhilarjun profile image
Akhil Arjun

Am glad it helps 😊

Collapse
 
samuelorobosa profile image
Samuel Amagbakhen

Wonderful post, mate

Collapse
 
akhilarjun profile image
Akhil Arjun

Glad you liked it !! 🕺

Collapse
 
technikhil314 profile image
technikhil314

if you want full greyscale dark mode then use invert(1) greyscalle(1)

Collapse
 
akhilarjun profile image
Akhil Arjun

Yes, but the only downside to it is that even the images go greyscale

Collapse
 
technikhil314 profile image
technikhil314

Yeah agreed.

Collapse
 
gavinsykes profile image
Gavin Sykes

Would that be such a bad thing? I've always thought having them do that would be pretty cool.

Thread Thread
 
akhilarjun profile image
Akhil Arjun

Oh! yes, it would be. I once worked for a client where the dark mode had everything greyscaled. It was awesome 😎 It's just that not every UX would work better that way. So it has to be informed decision

Collapse
 
sdktalks profile image
Sodeeq Olamide

This is very amazing, I'm surprised to realize to know that achieving dark mode on one's site is simpler than I thought

Collapse
 
akhilarjun profile image
Akhil Arjun

Well, they say a magician should never reveal his tricks. Maybe I am just a bad one 😂😀

Collapse
 
mdhesari profile image
Mohammad Fazel

Fantastic!

I shared it on my twitter account (putting credits).

Let me know if there's a copy right problem.

Thank you, peace.

Collapse
 
akhilarjun profile image
Akhil Arjun

Hey nopes. Have at it bro 😎✌

Collapse
 
photocoder profile image
Dimas

Hello!
That is amazing simple decision!
Great! Thank you very much.
But it is also converted all my background images...
Is it exist any option for filter them?

Collapse
 
akhilarjun profile image
Akhil Arjun

Yeah. You have to apply invert filter again on images to re-colorize them and turn back the hue a whole of 180deg.
We can do that by selecting all images so

html[theme='dark-mode'] img{
    filter: invert(1) hue-rotate(180deg);
}
Collapse
 
pau1phi11ips profile image
Paul Phillips

I think he was referring to CSS background-image not img's

Thread Thread
 
akhilarjun profile image
Akhil Arjun

I get it. Well, there are a couple of ways we can achieve that too. If it is just a div with a background-image set. Then applying filter would not be a problem there, but if that element has some content then the filter might affect it all too. So we might have to check for specialized solutions.
There is a beautiful article on css-tricks regarding the same css-tricks.com/apply-a-filter-to-a....

Hope that helps 😀

Thread Thread
 
photocoder profile image
Dimas

Yes, correct.
I found only the way to add extra "non-dark-mode" class to turn them back...

Some comments may only be visible to logged-in visitors. Sign in to view all comments.