Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 3, 2020 06:57 am GMT

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);}

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);}

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;}



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


Original Link: https://dev.to/akhilarjun/one-line-dark-mode-using-css-24li

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To