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
    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
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

Incredible Demos

  • By
    MooTools Zoomer Plugin

    I love to look around the MooTools Forge. As someone that creates lots of plugins, I get a lot of joy out of seeing what other developers are creating and possibly even how I could improve them. One great plugin I've found is...

  • By
    MooTools 1.3 Browser Object

    MooTools 1.3 was just released and one of the big additions is the Browser object.  The Browser object is very helpful in that not only do you get information about browser type and browser versions, you can gain information about the user's OS, browser plugins, and...

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!