Invert a whole webpage with CSS only

I recently saw Paul Irish’s jQuery invert page plugin. It inverts every color on a webpage including images or CSS. This reminded me of the invert color keyword that’s allowed on outlines (and sadly only supported by Opera and IE9+). So I wondered how …


This content originally appeared on Lea Verou’s blog and was authored by Lea Verou

I recently saw Paul Irish’s jQuery invert page plugin. It inverts every color on a webpage including images or CSS. This reminded me of the invert color keyword that’s allowed on outlines (and sadly only supported by Opera and IE9+). So I wondered how it could be exploited to achieve the same effect through CSS alone. Turned out to be quite simple actually:

body:before { content:“”; position:fixed; top:50%; left: 50%; z-index:9999; width:1px; height: 1px; outline:2999px solid invert; }

Not even pointer-events:none; is needed, since outlines don’t receive pointer events anyway, and there’s no issue with scrollbars since they don’t contribute to scrolling. So this is not even CSS3, it’s just plain ol’ CSS 2.1.

And here’s a bookmarklet to inject it into any given page: [Invert page](javascript:(function(){var%20style=document.createElement(‘style’);style.innerHTML=‘body:before%20{%20content:%22%22;%20position:fixed;%20top:50%25;%20left:50%25;%20z-index:9999;%20width:1px;%20height:%201px;%20outline:2999px%20solid%20invert;%20}’;document.body.appendChild(style)})();)

**Note:**This will only work on Opera and IE9+ since they’re currently the only ones supporting the color keyword ‘invert’ on outlines. However, it’s probably possible to add Firefox support too with SVG filters, since they support them on HTML elements as well.

As for why would someone want to invert a page… I guess it could be useful for people that can read white text on dark backgrounds more easily, April fools jokes, konami code fun and stuff like that.

Update: Mozilla is planning to never support invert because there’s a loophole in the CSS 2.1 spec that allows them to do that. However, you can push them to support it by voting on the relevant issue.


This content originally appeared on Lea Verou’s blog and was authored by Lea Verou


Print Share Comment Cite Upload Translate Updates
APA

Lea Verou | Sciencx (2011-04-14T00:00:00+00:00) Invert a whole webpage with CSS only. Retrieved from https://www.scien.cx/2011/04/14/invert-a-whole-webpage-with-css-only/

MLA
" » Invert a whole webpage with CSS only." Lea Verou | Sciencx - Thursday April 14, 2011, https://www.scien.cx/2011/04/14/invert-a-whole-webpage-with-css-only/
HARVARD
Lea Verou | Sciencx Thursday April 14, 2011 » Invert a whole webpage with CSS only., viewed ,<https://www.scien.cx/2011/04/14/invert-a-whole-webpage-with-css-only/>
VANCOUVER
Lea Verou | Sciencx - » Invert a whole webpage with CSS only. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2011/04/14/invert-a-whole-webpage-with-css-only/
CHICAGO
" » Invert a whole webpage with CSS only." Lea Verou | Sciencx - Accessed . https://www.scien.cx/2011/04/14/invert-a-whole-webpage-with-css-only/
IEEE
" » Invert a whole webpage with CSS only." Lea Verou | Sciencx [Online]. Available: https://www.scien.cx/2011/04/14/invert-a-whole-webpage-with-css-only/. [Accessed: ]
rf:citation
» Invert a whole webpage with CSS only | Lea Verou | Sciencx | https://www.scien.cx/2011/04/14/invert-a-whole-webpage-with-css-only/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.