Invert Colors Using CSS

By  on  

I've been obsessed lately with finding new or lesser known CSS properties and values and playing around when them.  Some of them are quite useful and others are seemingly only for a giggle.  CSS filters fall in the useful department:  grayscale, blur, sepia -- all common filters.  But how about invert?  And did you know you can invert the color of every element on a page?

The CSS

The invert value is percentage-based; 100% fully inverts the colors and 0% displays all colors as normal:

.normal {
	filter: invert(0%);
}

.inverted {
	filter: invert(100%);
}

You can invert individual elements or, if you invert the document.documentElement, the entire page contents get inverted.  The values reported back by window.getComputedStyle(el) will be the original CSS values, however, so there's no way of getting the true inverted values of given properties.

Color inversion is a useful accessibility tool, though it is oftentimes provided by the user's operating system or a separate tool.  I can see inversion being helpful at night, easing eye strain when looking at screens.  Can you think of a good use of color inversion?  Please share!

Recent Features

  • By
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

Discussion

  1. Interesting to see that a percentage is supported, I wouldn’t have expected that. Try inverting at 50% to see why.

    The upside of this is that values between 0 and 50% could be used for the purpose of desaturation.

    • Welp – that’s half inverted… so a percentage still makes sense it seems to me! :)

  2. Actually, I can think of a good use case for this: “night mode” for reading in low-light situations.

    I often use the inverted display mode of my iPhone when reading articles at bedtime. What’s annoying is that, of course, all the images invert, and I have to toggle (blast of light!) back for a moment to view the image.

    What would be useful, is to have a button for inverting the page *except* for the images. They could be dimmed as well (using opacity or some other filter).

    It’s probably not a huge use case, but it would be awesome if the browser could somehow detect if the device was in ‘inverted mode’.

    • Roy

      You can invert the entire page (body) and then invert the images back! E.g.:

      body {
          filter: invert(100%);
      },
      img: {
          filter: invert(100%);
      }
      
  3. Harrald

    Awesome! Didn’t knew that one. It also works on iOS 8 btw. So not only on the nightlies.

  4. I have tried this in one of my projet, it works well for chrome and firefox but it does NOT work in IE 10 and safari. Any solution for this ?

  5. Elixor

    I was thinking of inverting white or light colored logos (with a transparent background) for printing purposes!

  6. rickdog

    do you know of a way to color an object to be the reverse of it’s z-order underlying elements.
    For example, when drawing an black AOI box with black lines on top of a black background, the lines can’t be seen.

  7. This could be used to achieve a “night mode” effect on web pages but without affecting images (thumbnails and logo), videos, ad banners etc. Stuff like:

    body.invert:not(img) {
      ...invert code here...
    }
    
    • In macOS, when the Invert Colors setting is on, Pinterest’s desktop site inverts only the colors of the interface and not the images. How does it do that? How can a site know that the Invert Colors accessibility setting is turned on in the visitor’s operating system? And how could this have been done better? I.e. how can the site keep the Pinterest logo non-inverted as well? Thanks very much.

    • Jay

      probably it doesn’t ‘know’ – I’m guessing an invert(0%) !important is preventing the browser from inverting the images? I haven’t looked at their code though.

    • Hi! I wanted to use a button named “Night Mode” to revert colors on my website, but how do I do to link the CSS code to invert colors entered after the tag of my blog and this “Night Mode” HTML-made button? Thanks for your help ♥ :)

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