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
    Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

Incredible Demos

  • By
    MooTools: Set Style Per Media

    I'd bet one of the most used MooTools methods is the setStyle() method, which allows you to set CSS style declarations for an element. One of the limitations of MooTools' setStyle() method is that it sets the specific style for all medias.

  • By
    Duplicate the jQuery Homepage Tooltips Using Dojo

    The jQuery homepage has a pretty suave tooltip-like effect as seen below: Here's how to accomplish this same effect using Dojo. The XHTML The above HTML was taken directly from the jQuery homepage -- no changes. The CSS The above CSS has been slightly modified to match the CSS rules already...

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!