search
HomeWeb Front-endCSS TutorialLet's talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

UsingCSSHow to achieve the frosted glass effect? The following article will introduce to you how to use CSS to achieve frosted glass special effects (exploration of compatible solutions). I hope it will be helpful to you!

Let's talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

Some time ago, I used the filter effect of "Gaussian Blur" in a project. I tried a variety of solutions during the process. Here is a way to summarize, I hope it can help fellow Taoists in need~

UI Miss Sister insisted on letting me support the realization of what we commonly call the "frosted glass" effect on the customized Webview of the Android system. It was said that it must be achieved. If this effect is not achieved, the soul of the design will be lost. However, I explained it in every possible way, and she wanted it, wanted it, wanted it!

No way, let’s do some research!

[Recommended learning: css video tutorial]

1. Is background-filter a cheat?

The frosted glass effect is relatively common on iOS systems, such as message notification bars, mobile assistant cards, etc., so let’s open Apple’s official website and take a look!

Lets talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

As expected, the navigation bar uses the "Family Design" special effect of "frosted glass"

Open the console and copy the homework:

Lets talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

Mainly uses the properties of backdrop-filter CSS3, so I used it easily.

The main card CSS code is as follows:

.card-backdrop-filter {
    position: relative;
    z-index: 1;
    width: 600px;
    height: 300px;
    border-radius: 6px;
    padding: 10px;
    color: #fff;
    font-size: 16px;
    overflow: hidden;
    margin: 100px auto;
    backdrop-filter: blur(10px);
    background-color: rgba(255,255,255,0.72);
  }

The effect looks OK in the Chrome browser:

Lets talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

Then go to a certain station I tried it on the mobile version of the old Android version, but it didn’t work! ! !

Check the compatibility: backdrop-filter -- caiuse

Lets talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

emmm~, and then consider our own magic Changed Webview kernel situation. . .

We need to find another way. Let me add here. filter can be understood as a filter. backdrop-filter is to set a filter effect for the background. CSS currently The supported filter effects are

blur(): 模糊
brightness(): 亮度
contrast(): 对比度
drop-shadow(): 阴影
grayscale(): 灰度
hue-rotate(): 色相旋转
invert(): 反色
opacity(): 透明度
saturate(): 饱和度
sepia(): 褐色

If compatibility is not considered, backdrop-filter will directly display the background color on unsupported browsers, that is, the set effect will be lost (" Experience downgrade").

If Miss UI and Brother PM can agree, I highly recommend everyone to use it. After all, who doesn’t want to get off work early to study?

2. Filter, give it a try?

Another CSS property for setting blur is filter, so another idea we have is to simulate the backdrop-filter property through filter Effect.

And the compatibility of filter will be better: filter -- caiuse

Lets talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

Let’s see# The difference between the effects of ##filter and backdrop-filter:

/* filter 的写法,将 backdrop-filter 属性替换为 filter */
.card-filter {
  position: relative;
  z-index: 1;
  width: 600px;
  height: 300px;
  border-radius: 6px;
  padding: 10px;
  color: #fff;
  font-size: 16px;
  overflow: hidden; /* 隐藏超出元素区域外的内容 */
  margin: 100px auto;
  filter: blur(10px);
  background-color: rgba(255,255,255,0.72);
}

Lets talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

This effect is very different from the actual design requirements, so it needs to be modified. !

3. Combination punch of filter

Because

filter sets the fuzziness of the entire element rather than the background container for the element, so a and card is needed Placeholder elements of equal size are used to set the blur individually and serve as background elements.

3.1 ::before fixed double background image

I am lazy here and use pseudo elements directly

::before, in order to consider compatibility in actual scenarios, it is recommended that you still use div block-level element placeholder

.card-filter::before {
  content: ' ';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: -1; /* 放到当前 card-filter 元素的底部 */
  filter: blur(10px); /* 模糊度 */
  background: url(http://p2.qhimg.com/bdr/__85/t01781bd4b1218329e1.jpg) no-repeat center fixed;
  background-size: cover;
}

Note here that the card uses the same background image attribute as the entire container. The background image is fixed by

fixed, so that when the element is scrolled in any direction, the background image will not move, ensuring that the background effect is consistent

Lets talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

3.2 利用 margin 属性的负值扩大容器

模糊度的效果如下图,还是有差异,因为 filter 是从容器外边框向内聚合的一个滤镜,导致滤镜外边框有一个白圈

Lets talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

此时只需要扩大 ::before 元素的容器大小,这里直接可使用 margin 属性负值扩大容器

.card-filter::before {
  ...
+ margin: -20px;
}

此时的效果就有点“那个意思了”,看来快可以交差了

Lets talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

3.3 ::after 填补消失的背景色

原本设置在 .card-filter 类上的 background-color: rgba(255, 255, 255, 0.72); 没起作用!

因为 ::before 伪类作用于 .card-filter 元素内,是其子元素,又因为 ::beforebackground 属性设置了背景图,遮盖了父元素 .card-filter 的背景色。

知道了原因,那么我们就可以在 .card-filter 元素内再添加一个子元素(伪类),用于设置背景色!

Lets talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

再偷个懒,直接使用 ::after 伪类,就不用改造 DOM 结构。

.card-filter::after {
  content: ' ';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: -1;
  background-color: rgba(255, 255, 255, 0.72);
}

此时,效果就和 backdrop-filter 的效果相同:

1Lets talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

放到“自研”的 Webview 内核中也能看到效果了!

四、总结

总的来说,如果能说服产品经理和 UI 小姐姐的话,咱就用 backdrop-filter,说服不了就用 filter 的组合拳模拟吧!

另外 backdrop-filter 属性是有性能问题的,咱就是说,都 2022 年了,求求大家升级一下手上的设备吧!,兼容性真让前端工程师秃头儿!

Lets talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions)

如果“毛玻璃”和背景没有文中的相对移动,直接让 UI 小姐姐切个图,直接解决一切兼容性 & 性能问题!

在研究过程中,笔者还尝试过 SVG 的 feGaussianBlur 标签,效果和 filter 一样,会稍微复杂一些,不过也是个可施行的方案,大家可自行尝试下~

原文地址:https://juejin.cn/post/7070325873202692104

(学习视频分享:web前端

The above is the detailed content of Let's talk about how to use CSS to achieve frosted glass effects (exploration of compatibility solutions). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools