htmlcssgoogle-chromeimage-masking

Mask in Chrome combining position absolute, overflow hidden, z-index and border-radius


I am trying to achieve an effect, where I will have div with background image with content inside. This content should be flex-positioned div with the same image but blurred (while keeping its relative position to parent).

In search for salvation, I have discovered that certain combination of CSS rules make this exact effect, but only in Chrome.

.container {
  width: 320px;
  height: 240px;
  position: relative; /* it is required */
  display: flex; /* it is required */
  background: url(https://loremflickr.com/cache/resized/4848_46406748211_5572c760e0_320_240_nofilter.jpg);
}

.mask {
  z-index: 1; /* it is required */
  overflow: hidden; /* it is required */
  width: 150px;
  height: 150px;
  border-radius: 1px; /* it is required */
}

.element {
  background: url(https://loremflickr.com/cache/resized/4848_46406748211_5572c760e0_320_240_nofilter.jpg);
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  filter: blur(5px);
}


<div class="container">
  <div class="mask">
    <div class="element"></div>
  </div>
</div>

https://jsfiddle.net/39um580g/16/

Chrome / Chrome Mobile: enter image description here

Firefox: enter image description here

Safari: enter image description here

This is ridiculous. Is there a way I can make this solution cross-browser?

Answer to Moorthy G:

Let's suppose my block is placed on the right side. Current behavior of Chrome version is as I desire: enter image description here

Adding any kind of transform will make .mask relative, so it will destroy desired effect: enter image description here


Solution

  • Adding clip-path: polygon(0px 0px, 0px 100%, 100% 100%, 100% 0); clips your mask.

    .mask {
      z-index: 1; /* it is required */
      overflow: hidden; /* it is required */
      width: 150px;
      height: 150px;
      border-radius: 1px; /* it is required */      
      clip-path: polygon(0px 0px, 0px 100%, 100% 100%, 100% 0); /* Fix for Firefox*/
    }