CSS implements frosted glass style
To achieve a frosted glass background, you can use CSS3 ::before
pseudo-elements and backdrop-filter
properties, combined with opacity
properties and blur()
functions.
The specific implementation steps are as follows:
- Create an element with a background, such as a
div
element.
div {
background-image: url("your-image-url");
}
- Use
::before
the pseudo-element to add a semi-transparent background layer to the element.
div::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.5);
}
In this example, ::before
the content of the pseudo-element is set to empty, the position is absolute, the width and height are 100%, and a semi-transparent white background color is set.
- Use
backdrop-filter
the attribute::before
to blur the background of the pseudo-element.
div::before {
/* ... */
backdrop-filter: blur(10px);
}
In this example, use blur()
the function to set the blur level to 10 pixels. You can adjust this value as needed to control the blurriness.
- To make the frosted glass effect more pronounced, the opacity of the pseudo-element can
opacity
be reduced via the attribute .::before
div::before {
/* ... */
opacity: 0.8;
}
In this example, ::before
lower the opacity of the pseudo-element to 0.8 to enhance the frosted glass effect.
The complete CSS code is as follows:
div {
background-image: url("your-image-url");
position: relative;
}
div::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.5);
backdrop-filter: blur(10px);
opacity: 0.8;
}
In this way, a frosted glass background effect can be achieved quickly and easily using CSS. It should be noted that backdrop-filter
attributes are not supported by all browsers, and compatibility testing and compatibility processing are required.