CSS Backdrop Filter

The backdrop-filter CSS property lets you apply graphical effects (like blurring, color shifting, or compositing) to the area behind an element. This is different from filter, which applies effects to the element itself.

Here's a comprehensive breakdown:

Key Concepts:

Effect on the Backdrop: backdrop-filter affects the area behind the element, allowing you to blur, tint, or otherwise manipulate the content underneath without directly affecting the element itself. This is often used to create glass-like or frosted-glass effects.

Stacking Context: The element with backdrop-filter must have a stacking context (other than the root element). This usually means it needs to have its position set to something other than static, or a z-index value other than auto.

Compositing: The backdrop is a composite of all the content behind the element.

Basic Syntax:

element {
  backdrop-filter: <filter-function> [<filter-function>]* | none;
}
  

<filter-function> Options:

This is where the real power lies. You can use one or more of the following filter functions:

blur(<length>): Applies a Gaussian blur to the backdrop. <length> specifies the radius of the blur. A larger value creates a stronger blur.

backdrop-filter: blur(5px);

brightness(<number> | <percentage>): Adjusts the brightness of the backdrop.

backdrop-filter: brightness(150%); /* Increases brightness */

contrast(<number> | <percentage>): Adjusts the contrast of the backdrop.

backdrop-filter: contrast(80%); /* Decreases contrast */

grayscale(<number> | <percentage>): Converts the backdrop to grayscale.

backdrop-filter: grayscale(100%);

hue-rotate(<angle>): Applies a hue rotation to the backdrop. <angle> defines the number of degrees around the color circle the hue will be shifted.

backdrop-filter: hue-rotate(90deg); /* Shifts the hue */

invert(<number> | <percentage>): Inverts the colors of the backdrop.

backdrop-filter: invert(100%);

opacity(<number> | <percentage>): Adjusts the opacity of the backdrop.

backdrop-filter: opacity(50%); /* Makes the backdrop semi-transparent */

saturate(<number> | <percentage>): Adjusts the saturation of the backdrop.

backdrop-filter: saturate(200%); /* Increases saturation */

sepia(<number> | <percentage>): Applies a sepia effect to the backdrop.

backdrop-filter: sepia(80%);

url(<url>): Applies an SVG filter to the backdrop. The points to an SVG file containing a <filter> element. This is a more advanced technique for complex effects.

backdrop-filter: url("filters.svg#myFilter");

none: Removes any previously applied backdrop filters.

backdrop-filter: none;

Combining Filters:

You can chain multiple filter functions together to create complex effects:

backdrop-filter: blur(10px) brightness(80%) saturate(120%);

Example: Frosted Glass Effect

This is a very common use case:

 
<style>
  .c-body {
    background-image: url("https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mnx8YmVhdXRpZnVsJTIwbGFuZHNjYXBlfGVufDB8fDB8fHww&w=1000&q=80");
    background-size: cover;
    background-repeat: no-repeat;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: sans-serif;
  }

  .glass-panel {
    background: rgba(255, 255, 255, 0.2); /* Semi-transparent white */
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px); /* The magic! */
    -webkit-backdrop-filter: blur(10px); /* For Safari compatibility */
    position: relative; /* Creates a stacking context */
    width: 300px;
    text-align: center;
    color: #333;
  }
</style>
 <div class="c-body">
  <div class="glass-panel">
    <h1>Frosted Glass</h1> 
  </div>
 </div>
  

Frosted Glass

Explanation of the Frosted Glass Example:

Background: Sets up a background image on the body.

glass-panel Class:

  • background: rgba(255, 255, 255, 0.2);: Sets a semi-transparent white background on the panel itself. This is crucial for the frosted effect. Without it, you'd just see the blurry background directly.
  • backdrop-filter: blur(10px);: Applies the blur to the backdrop (the area behind the panel).
  • -webkit-backdrop-filter: blur(10px);: Adds the vendor prefix for older versions of Safari and some other WebKit-based browsers. It's generally good practice to include the -webkit- prefix for maximum compatibility, although most modern browsers support the unprefixed version.
  • position: relative;: Creates a stacking context. This is essential for backdrop-filter to work. Other values like absolute or fixed would also work.
  • box-shadow: Adds a subtle shadow for visual depth.

Important Considerations and Browser Compatibility:

Browser Support: backdrop-filter is supported by most modern browsers (Chrome, Firefox, Safari, Edge). However, older browsers (especially older versions of Internet Explorer) do not support it. Check caniuse.com for the latest browser support information.

Performance: backdrop-filter can be computationally expensive, especially with large blur radii. Use it sparingly and test performance on different devices. Consider using simpler effects if performance is a concern.

Stacking Context: As mentioned earlier, the element with backdrop-filter must have a stacking context (e.g., position: relative;, position: absolute;, or a z-index value other than auto). If it doesn't, the filter won't be applied.

-webkit- Prefix: For broader Safari compatibility (especially older versions), include the -webkit-backdrop-filter prefix:

backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px); /* Safari */

Fallback: If you need to support older browsers that don't support backdrop-filter, provide a fallback. The most common fallback is a semi-transparent background color.

.glass-panel {
  background: rgba(255, 255, 255, 0.5); /* Fallback for browsers without backdrop-filter */
  backdrop-filter: blur(10px);
}
  

Content Behind: Make sure there's something behind the element for the backdrop-filter to affect. If the element is at the top of the stacking order, there might be nothing behind it to filter.

In summary, backdrop-filter is a powerful CSS property for creating visually appealing effects, particularly frosted glass and similar designs. Remember to consider browser compatibility, performance, and the need for a stacking context when using it.