Dark Mode in One Line of Code!

By  on  

Dark mode has seemingly become the desired visual mode for websites and mobile apps alike. Dark mode is easier on the eyes, especially for those like me who like to burn the midnight oil by coding and reading tutorials. Unfortunately not all websites offer dark mode, so it's up to me to remedy the situation.

Though it's not a true "dark mode", you can use CSS' filter to create dark mode of your own:

html {
  filter: invert(1);
}

Inverting colors completely via 1 will make that light-themed website much more comfortable on your eyes. It's important to realize that developers shouldn't consider this a long-term solution, as it's a quite lazy remedy and doesn't lend well to branding.

Recent Features

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    Duplicate the jQuery Homepage Tooltips

    The jQuery homepage has a pretty suave tooltip-like effect as seen below: The amount of jQuery required to duplicate this effect is next to nothing;  in fact, there's more CSS than there is jQuery code!  Let's explore how we can duplicate jQuery's tooltip effect. The HTML The overall...

  • By
    Geolocation API

    One interesting aspect of web development is geolocation; where is your user viewing your website from? You can base your language locale on that data or show certain products in your store based on the user's location. Let's examine how you can...

Discussion

  1. Nice trick !

    I would add exactly the same on all images with

    img {
    filter: invert(1);
    }

    Then you are even closer to a dark mode ;)

  2. Adilbim

    I just tried it here and it worked, Thanks for the info.

    here is the code I used:

    document.querySelector('html').style.filter = 'invert(1)'

  3. Alex

    I love filter for this! You can also make the inversion less harsh by adding to the filter list:

    /* Use hue-rotate for optional color adjustment */
    html { filter: invert(1) contrast(0.95) saturate(0.5) hue-rotate(180deg);
    
  4. Daniel

    Never EVER use this trick other than for a quick preview.

    CSS filters on large areas are really bad for scrolling performance.

  5. This isn’t quite dark mode. This is just an invert which makes images look like crap. Still, neat trick.
    Thanks

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!