css中filter属性和backdrop-filter对比有什么区别
我们定义了一个container元素div,子元素有content和filter两个div元素。
body{ margin:0; padding:0; } .container{ width:100%; height:100vh; display:flex; align-items:center; justify-content:center; background-color:aqua; } .content{ position:absolute; bottom:40%; width:300px; height:300px; background-color:blueviolet; } .filter{ position:absolute; bottom:0; width:100%; height:50%; font-size:32px; z-index:20; }
以上元素,我们可以得到如下布局:
这时候,我们将filter元素改为
.filter{ position:absolute; bottom:0; width:100%; height:50%; filter:blur(5px); z-index:20; font-size:32px; }
从代码看,我们添加了filter: blur(5px)。如下图展示效果,我们发现filter元素div和其中的文字内容,都被模糊化了。
但如果如下修改样式
.filter{ position:absolute; bottom:0; width:100%; height:50%; backdrop-filter:blur(5px); z-index:20; font-size:32px; }
使用backdrop-filter: blur(5px)元素,则得到如下图排版
我们发现,只有filter元素DIV被模糊化,而子内容文字并没有受到任何影响。
.filter{ position:absolute; bottom:0; width:100%; height:50%; background-color:chocolate; backdrop-filter:blur(5px); z-index:20; font-size:32px; }
但是,如果按照以上代码,给filter元素设置了背景色background-color: chocolate,这时候,就几乎看不到模糊化的效果。
或者,我们把content元素DIV背景色去除,
.content{ position:absolute; bottom:40%; width:300px; height:300px; }
这就是为什么说,为了看到效果,必须使元素或其背景至少部分透明。
我们发现,backdrop-filter属性还只能在部分最新版浏览器上有效果,所以目前来看,此属性的兼容性较差。
以上就是关于“css中filter属性和backdrop-filter对比有什么区别”的内容,如果改文章对你有所帮助并觉得写得不错,劳请分享给你的好友一起学习新知识,若想了解更多相关知识内容,请多多关注恰卡编程网行业资讯频道。