CSS implements frosted glass style

CSS implements frosted glass style

To achieve a frosted glass background, you can use CSS3 ::beforepseudo-elements and backdrop-filterproperties, combined with opacityproperties and blur()functions.

The specific implementation steps are as follows:

  1. Create an element with a background, such as a divelement.
div {
    
    
  background-image: url("your-image-url");
}
  1. Use ::beforethe 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, ::beforethe 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.

  1. Use backdrop-filterthe attribute ::beforeto 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.

  1. To make the frosted glass effect more pronounced, the opacity of the pseudo-element can opacitybe reduced via the attribute .::before
div::before {
    
    
  /* ... */
  opacity: 0.8;
}

In this example, ::beforelower 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-filterattributes are not supported by all browsers, and compatibility testing and compatibility processing are required.

Guess you like

Origin blog.csdn.net/yzh648542313/article/details/130751692