Can you CSS Blur based on a gradient mask?
Asked Answered
C

3

23

Can you CSS Blur based on a gradient mask?

Something similar to this effect, http://www.imagemagick.org/Usage/mapping/#blur?

Channel answered 18/12, 2013 at 2:56 Comment(3)
May or may not be possible, but seems only in WebKit.Ternion
I only need it in webkit :) will look into your links, thanks.Channel
I wrote a tutorial about this here: smashingmagazine.com/2024/01/css-blurry-shimmer-effectKenon
F
25

This can help you http://codepen.io/iamvdo/pen/xECmI

.effet{
  width: 400px; height: 300px;
  margin: 0 auto 50px auto;
  box-shadow: 0 1px 5px rgba(0,0,0,.5);
}
.effet img{
  position: absolute;
}
.filtre--r{
  -webkit-mask: -webkit-radial-gradient( center, closest-side, transparent 30%, black 80%);
  -webkit-mask: radial-gradient( closest-side at center, transparent 50%, black 110%);
  -webkit-filter: blur(5px);
  mask: url('#mask-radial');
  filter: url('#filtre1');
}
.filtre--l{
  -webkit-mask: -webkit-linear-gradient(black, transparent 30%, black);
  -webkit-mask: linear-gradient(black, transparent 30%, black);
  -webkit-filter: blur(3px);
  mask: url('#mask-linear');
  filter: url('#filtre2');
}
.filtre:hover{
   -webkit-mask: none;
   -webkit-filter: none;
   mask: none;
   filter: none;
}
p{
   text-align: center;
   color: rgba(0,0,0,.6);
   margin: 1em;
}
p a{
  color: rgba(0,0,0,.6);
}
<p><strong>Radial</strong> progressive blur</p>
<div class="effet">
  <img src="http://css3create.com/squelettes/images/articles/flou-localise-1.jpg" alt="" />
<img class="filtre filtre--r" src="http://css3create.com/squelettes/images/articles/flou-localise-1.jpg" alt="" />
</div>
<p><strong>Linear</strong> progressive blur</p>
<div class="effet">
<img src="http://css3create.com/squelettes/images/articles/flou-localise-2.jpg" alt="" />
<img class="filtre filtre--l" src="http://css3create.com/squelettes/images/articles/flou-localise-2.jpg" alt="" />
</div>
<p>Hover over images to see without blur</p>
<p>Next demo: <a href="http://codepen.io/iamvdo/pen/djEBu" target="_blank">iOS 7 background blur with CSS</a></p>
<svg height="0">
<defs>
  <mask id="mask-radial">
    <rect width="400" height="300" fill="url(#g1)"></rect>
    <radialGradient id="g1" cx="50%" cy="50%" r="50%">
      <stop stop-color="black" offset="50%"/>
      <stop stop-color="white" offset="110%"/>
    </radialGradient>
  </mask>
  <mask id="mask-linear">
    <rect width="400" height="300" fill="url(#l1)"></rect>
    <linearGradient id="l1" x1="0" y1="0" x2="0" y2="1">
      <stop stop-color="white" offset="0%"/>
      <stop stop-color="black" offset="30%"/>
      <stop stop-color="white" offset="100%"/>
    </linearGradient>
  </mask>
  <filter id="filtre1">
    <feGaussianBlur in="SourceGraphic" stdDeviation="5"/>
  </filter>
  <filter id="filtre2">
    <feGaussianBlur in="SourceGraphic" stdDeviation="3"/>
  </filter>
</defs>
</svg>
Festoon answered 24/12, 2015 at 21:37 Comment(1)
So this works by having a blurred image, a normal image, and then gradient masking between the two. Cool solution but I wish you could blur based on dynamic content such as video or carousels, but filters don't seem to affect the layers below them.Onanism
U
19

[EDIT] As of October 2022, it is now possible in all the modern browsers.


Yes you can, but at the moment the backdrop blur is not supported on all the browsers.

Here is a simple example working on Chrome and Safari but not Firefox (because of the lack of backdrop-filter support). https://codepen.io/antoniandre/pen/vYpWQXd?editors=0100

* {margin: 0}

body {
  height: 100vh;
  background: url('https://images.unsplash.com/photo-1500042600524-37ecb686c775?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2070&q=80') 0 0 / cover;

  &:after {
    content: '';
    position: absolute;
    inset: 10%;
    backdrop-filter: blur(20px);
    border: 1px solid white;
    -webkit-mask: linear-gradient(90deg, transparent, black 50%);
  }
}
Ulna answered 3/4, 2022 at 8:26 Comment(6)
Did firefox just add this? I opened your codepen in Chrome and Firefox and I dont see any differences.Windham
Indeed they did! :tada:Ulna
Firefox on android doesn't support this yet.Mopboard
Works for me on Firefox 122.0b3Chetnik
Yes it's now supported everywhere (like the big heading says) ;)Ulna
If it's not working for you: Remove overflow: hidden; from the parent. Took me a while to figure this out...Marksman
P
-3

You might nowadays simply use:

backdrop-filter: blur(4px)

All modern browsers except Firefox support it.

Precondemn answered 31/3, 2021 at 8:1 Comment(1)
I believe this is insufficient for the poster's needs because it will blur the entire intersection of the background and what's underneath it, not in a gradient.Heedless

© 2022 - 2024 — McMap. All rights reserved.