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
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

Incredible Demos

  • By
    Use Elements as Background Images with -moz-element

    We all know that each browser vendor takes the liberty of implementing their own CSS and JavaScript features, and I'm thankful for that. Mozilla and WebKit have come out with some interesting proprietary CSS properties, and since we all know that cementing standards...

  • 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?

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!