CSS Filter not working on body in Chrome & Firefox

坚强是说给别人听的谎言 提交于 2021-01-29 04:50:53

问题


I want to create a chrome extension that does some filtering for color blindness testing and wanted to use css/svg filters but have a problem with filters not affecting body background.

Any suggestions?

Link to codepen (test in Chrome) or see screenshots below: http://codepen.io/larsenwork/pen/oZvdzX/?editors=1100

html {
	filter: grayscale(100%);
}

body, div {
	background-color: cyan;
}



/* Codepen style (ignore) */
* {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}

body { 
	min-height: 100vh;
	padding: calc(3vh + 3vw);
	display: flex;
	flex-direction: column;
	align-items: flex-start;
	font-family: sans-serif;
	font-weight: 700;
	font-size: calc(3vh + 3vw);
	line-height: 1.3;
}

div {
	margin: 0 calc(-.5vh - .5vw);
	padding: 0 calc(.5vh + .5vw);
}

small { 
	font-size: .6em;
	margin-top: 2em;
}

pre {
	font-weight: normal;
}
In Chrome: Why is the body bg not gray
<div>When this is?</div>





<small>Same result (i.e. not applied to body) with e.g. <pre>filter: hue-rotate(90deg);</pre>or<pre>filter: url("#grayscale");</pre></small>

<!-- Used for alt filter test -->
<svg version='1.1' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns='http://www.w3.org/2000/svg'><filter id='grayscale'><feColorMatrix type='saturate' values='0'/></filter></svg>

Chrome + Firefox:

Safari:


回答1:


So I found a solution (not happy with it) but setting a background property like background-color: magenta; or background-image: url(""); on the html fixes the issue in chrome and Firefox

Link to working version: http://codepen.io/larsenwork/pen/mWbjJY?editors=1100

html {
    filter: grayscale(100%);
    background-color: magenta;
}

body, div {
    background-color: cyan;
}


来源:https://stackoverflow.com/questions/42400970/css-filter-not-working-on-body-in-chrome-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!