[英]CSS filter:invert not working with background-color
I have the following CSS applied to the <html>
tag: filter: invert(1);
我将以下 CSS 应用于
<html>
标签: filter: invert(1);
All elements get inverted even background-color
of elements, (Chrome v80)所有元素都被反转,甚至元素的
background-color
,(Chrome v80)
Once I try the same on Safari iOS 13 / Safari MacOS, every element get inverted except for <html>'s background-color
一旦我在 Safari iOS 13 / Safari MacOS 上尝试相同的操作,除 <html> 的
background-color
外,每个元素都会反转
CSS filter property is supported for all browsers I'm running:我正在运行的所有浏览器都支持 CSS 过滤器属性:
https://caniuse.com/#feat=mdn-css_properties_filter . https://caniuse.com/#feat=mdn-css_properties_filter 。
Could someone explain this behavior?有人可以解释这种行为吗?
html { filter: invert(1); /* this background-color does not change on ios+other browsers */ background: #fff; padding: 50px; } body { background-color: #0000ff; display: flex; align-items: center; justify-content: center; height: 100vh; }.text { text-align: center; color: red; }
<div class="text"> If it works: color should not be red, background should not be blue </div>
html
element. html
元素。 The easy workaround is to block body
's background propagation to the document's canvas, but make it take the same size as the html
by removing its margin, and applying all the styles you had on html
on the body, and the ones you had on the body
to a wrapper <div>
. The easy workaround is to block
body
's background propagation to the document's canvas, but make it take the same size as the html
by removing its margin, and applying all the styles you had on html
on the body, and the ones you had on body
到包装器<div>
。
html { /* block body's background propagation */ background: #FFF; } /* move all one layer down */ body { filter: invert(1); background: #fff; padding: 50px; /* make it cover the full canvas */ margin: 0; }.wrapper { background-color: #0000ff; display: flex; align-items: center; justify-content: center; height: 100vh; }.text { text-align: center; color: red; }
<div class="wrapper"> <div class="text"> If it works: color should not be red, background should not be blue and border should not be white </div> </div>
There are a few concepts at play here, and their interaction is not that easy to grasp (at least to me...).这里有一些概念在起作用,它们的交互并不是那么容易掌握(至少对我来说......)。
"Background propagation" : Some special elements have special behaviors regarding their CSS background
property.“背景传播” :一些特殊元素在其 CSS
background
属性方面具有特殊行为。 Notably, html
and body
may give their own background to the "document's canvas".值得注意的是,
html
和body
可能会为“文档的画布”提供自己的背景。 The basic workflow is基本工作流程是
html
's background is not none
and not transparent
, use that for "document's canvas".html
的背景不是none
且transparent
,则将其用于“文档画布”。body
's background is not none
and not transparent
, use that for "document's canvas".body
的背景不是none
且不是transparent
,则将其用于“文档的画布”。 "Post-Processing" effects like filter
and opacity
should apply on a whole "rendering layer" when all its inner content already has been rendered.当所有内部内容已经被渲染时,
filter
和opacity
等“后处理”效果应该应用于整个“渲染层”。
Now, it's very unclear how the "document's canvas" should be affected by these "post-processing" effects, and I couldn't find any definitive answer to this case in the specs.现在,还不清楚“文档的画布”应该如何受到这些“后处理”效果的影响,而且我在规范中找不到任何明确的答案。
What's for sure, is that we have [Compat] issues in there...可以肯定的是,我们有 [Compat] 问题...
Not only not all browsers do follow the same rules, but some browsers will behave differently when the page is presented as a standalone window, or in an iframe.不仅并非所有浏览器都遵循相同的规则,而且当页面呈现为独立的 window 或 iframe 时,某些浏览器的行为也会有所不同。
Since the test results do vary between windowed and framed renderings, and that StackSnippet only allows framed rendering, I am forced to outsource the test case in this plnkr .由于测试结果在窗口渲染和框架渲染之间确实有所不同,并且 StackSnippet 只允许框架渲染,所以我不得不在这个 plnkr中外包测试用例。
html {
background: red;
height: 50vh;
border: 10px solid green;
}
.opacity {
opacity: 0.5;
}
.filter {
filter: invert(1);
}
body {
background: yellow;
margin: 10vh;
border: 2px solid green;
}
The results from these tests for majors browsers are:这些针对主要浏览器的测试结果如下:
When windowed : (screenshot orders, from left to right: nothing , filter , opacity , filter + opacity ).窗口化时:(屏幕截图顺序,从左到右:无、过滤器、不透明度、过滤器+不透明度)。
<html>
when opacity is set and applies both opacity and filter on the <html>
's background.<html>
创建一个新层,并在<html>
的背景上应用不透明度和过滤器。<body>
's background color as the document's canvas... but let it unaffected by the filter.<body>
的背景颜色作为文档的 canvas... 但让它不受过滤器的影响。 When framed : (screenshot orders, from left to right: nothing , filter , opacity , filter + opacity ).加框时:(截图顺序,从左到右:无、滤镜、不透明度、滤镜+不透明度)。
<html>
on which the opacity is applied.<html>
创建一个应用不透明度的新层。<html>
when opacity is set and applies both opacity and filter on the <html>
's background.<html>
创建一个新层,并在<html>
的背景上应用不透明度和过滤器。 So once again, I don't know if any result here is per specs, what I know is that as web-authors, we should avoid messing with it when possible.所以再一次,我不知道这里的任何结果是否符合规范,我知道作为网络作者,我们应该尽可能避免弄乱它。
Post-scriptum:后记:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.