DEV Community

Gerardo Andrés Ruiz Castillo
Gerardo Andrés Ruiz Castillo

Posted on • Originally published at geanruca.gitvlg.com

Improving UI Blur Effects with Multi-Panel Frames

Introduction

Achieving consistent and visually appealing blur effects in web UIs can be tricky. Browser inconsistencies and unexpected stacking contexts often lead to broken or unreliable implementations. This post details how the devlog-ist/landing project addressed these challenges by replacing a problematic clip-path approach with a more robust multi-panel frame technique for blurring backgrounds.

The Problem: Inconsistent Clip-Path Blur

The initial implementation attempted to create a blur effect around a target element by using a CSS clip-path with the evenodd rule to create a cutout. The backdrop-filter was then applied to this clipped area. However, this approach proved unreliable due to the clip-path failing to consistently create the desired cutout across different browsers and stacking contexts. The result was an inconsistent blur effect that sometimes failed to exclude the target element.

The Solution: Four-Panel Frame with Backdrop Filter

To address the inconsistencies, the implementation was refactored to use a four-panel frame approach. This involves creating four positioned div elements (top, right, bottom, left) around the target element. Each div has its own backdrop-filter applied, creating the blur effect on the surrounding area while reliably excluding the target element from the blur, regardless of stacking context. This method ensures a consistent blur effect across various browsers and scenarios.

Here's a simplified example of the CSS structure:

.blur-container {
  position: relative;
}

.blur-panel {
  position: absolute;
  backdrop-filter: blur(10px);
  /* Additional styling for positioning and size */
}

.blur-panel-top {
  top: 0;
  left: 0;
  width: 100%;
  height: some-height;
}

/* Similar styles for .blur-panel-right, .blur-panel-bottom, .blur-panel-left */

.target-element {
  position: relative; /* Ensure it's above the blur panels */
  /* Other styles */
}
Enter fullscreen mode Exit fullscreen mode

In this example, .blur-container wraps both the blur panels and the target element. Each .blur-panel is positioned absolutely to create the frame, and the backdrop-filter applies the blur. The target-element is also positioned relatively to ensure it appears above the blur panels.

Benefits of the New Approach

  • Reliability: The four-panel frame consistently excludes the target element from the blur, resolving the issues with the previous clip-path approach.
  • Cross-Browser Compatibility: This method works reliably across different browsers, providing a more uniform user experience.
  • Flexibility: The size and position of the blur effect can be easily adjusted by modifying the dimensions of the four panels.

Key Takeaway

When implementing UI effects like blurs, prioritize robustness and cross-browser compatibility. Simple, direct approaches like the multi-panel frame often provide more reliable results than complex CSS tricks like clip-path, especially when dealing with stacking contexts and browser inconsistencies.

Top comments (0)