【问题标题】:How can I make a CSS glass/blur effect work for an overlay?如何使 CSS 玻璃/模糊效果适用于叠加层?
【发布时间】:2015-02-19 10:41:27
【问题描述】:

我无法在半透明叠加 div 上应用模糊效果。我希望 div 后面的所有内容都变得模糊,如下所示:

这是一个不起作用的 jsfiddle:http://jsfiddle.net/u2y2091z/

任何想法如何使这项工作?我想让它尽可能简单,并让它成为跨浏览器。这是我正在使用的 CSS:

#overlay {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;

    background:black;
    background:rgba(0,0,0,0.8);

    filter:blur(4px);
    -o-filter:blur(4px);
    -ms-filter:blur(4px);
    -moz-filter:blur(4px);
    -webkit-filter:blur(4px);
}

【问题讨论】:

  • 也许使 div 不透明,但使用图像作为背景的伪元素,可以独立模糊。
  • @chipChocolate.py 默认情况下,FF35+ 支持 CSS 过滤器。但我同意你的看法,我们作为开发人员不应该依赖它,因为它不是跨浏览器功能。

标签: html css blur css-filters


【解决方案1】:

更简单和最新的答案:

backdrop-filter: blur(6px);

注意browser support 并不完美,但在大多数情况下,模糊是不必要的。

【讨论】:

  • 这是迄今为止最好的答案。
  • 请注意,截至 2021-02-01,目前没有 Firefox 版本支持此功能。
【解决方案2】:

这是一个使用 svg 过滤器的示例。

想法是使用svg 元素和height#overlay 相同,并在其上应用feGaussianblur 过滤器。此过滤器应用于 svg image 元素。要使其具有突出效果,您可以在叠加层的底部使用box-shadow

Browser Support for svg filters.

Codepen上的演示

body {
  background: #222222;
}
#container {
  position: relative;
  width: 450px;
  margin: 0 auto;
}
img {
  height: 300px;
}
#overlay {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 1;
  color: rgba(130, 130, 130, 0.5);
  font-size: 50px;
  text-align: center;
  line-height: 100px;
  box-shadow: 0 3px 5px rgba(0, 0, 0, 0.3);
}
<div id="container">
  <img src="http://lorempixel.com/450/300/sports" />
  <div id="overlay">WET</div>
  <svg width="450" height="100" viewBox="0 0 450 100" style="position: absolute; top: 0;">
    <defs>
      <filter id="blur">
        <feGaussianBlur in="SourceGraphic" stdDeviation="3" />
      </filter>
    </defs>
    <image filter="url(#blur)" xlink:href="http://lorempixel.com/450/300/sports" x="0" y="0" height="300px" width="450px" />
  </svg>
</div>

【讨论】:

    【解决方案3】:

    我能够将这里每个人的信息拼凑起来并进一步谷歌搜索,我想出了以下适用于 Chrome 和 Firefox 的信息:http://jsfiddle.net/xtbmpcsu/。我仍在努力使这项工作适用于 IE 和 Opera。

    关键是将内容放入应用过滤器的div中:

    body {
        background: #300000;
        background: linear-gradient(45deg, #300000, #000000, #300000, #000000);
        color: white;
    }
    #mask {
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        background-color: black;
        opacity: 0.5;
    }
    img {
        filter: blur(10px);
        -webkit-filter: blur(10px);
        -moz-filter: blur(10px);
      -o-filter: blur(10px);
      -ms-filter: blur(10px);
        position: absolute;
        left: 100px;
        top: 100px;
        height: 300px;
        width: auto;
    }
    <div id="mask">
        <p>Lorem ipsum ...</p>
        <img src="http://www.byui.edu/images/agriculture-life-sciences/flower.jpg" />
    </div>

    所以 mask 应用了过滤器。另外,请注意 url() 用于过滤器,该过滤器的值带有 &lt;svg&gt; 标记——这个想法来自 http://codepen.io/AmeliaBR/pen/xGuBr。如果您碰巧缩小了 CSS,您可能需要将 SVG 过滤器标记中的任何空格替换为“%20”。

    所以现在,掩码 div 内的所有内容都被模糊了。

    【讨论】:

    • 太聪明了
    • Chrome 不喜欢最终的filter: url(...。删除它,Chrome 会成功使用filter: blur(10px);
    • 我误会了吗?这不是OP想要的。文字不应模糊。
    • @Eric,您显然误解了,因为回答者 OP。
    【解决方案4】:
    background: rgba(255,255,255,0.5);
    backdrop-filter: blur(5px);
    

    不是为您的内容添加另一个模糊背景, 你可以使用backdrop-filter。仅供参考 IE 11 和 Firefox 可能不支持它。检查caniuse

    演示:

    header {
      position: fixed;
      width: 100%;
      padding: 10px;
      background: rgba(255,255,255,0.5);
      backdrop-filter: blur(5px);
    }
    body {
      margin: 0;
    }
    <header>
      Header
    </header>
    <div>
      <img src="https://dummyimage.com/600x400/000/fff" />
      <img src="https://dummyimage.com/600x400/000/fff" />
      <img src="https://dummyimage.com/600x400/000/fff" />
    </div>

    【讨论】:

    【解决方案5】:

    如果您今天正在寻找一种可靠的跨浏览器方法,那么您将找不到一个很好的方法。您拥有的最佳选择是创建两个图像(这在某些环境中可能是自动化的),并将它们排列成一个覆盖另一个。我在下面创建了一个简单的示例:

    <figure class="js">
        <img src="http://i.imgur.com/3oenmve.png" />
        <img src="http://i.imgur.com/3oenmve.png?1" class="blur" />
    </figure>
    
    figure.js {
        position: relative;
        width: 250px; height: 250px;
    }
    
    figure.js .blur {
        top: 0; left: 0;
        position: absolute;
        clip: rect( 0, 250px, 125px, 0 );
    }
    

    虽然有效,但即使是这种方法也不一定是理想的。话虽如此,它确实产生了desired result

    【讨论】:

      【解决方案6】:

      这是一个可能的解决方案。

      HTML

      <img id="source" src="http://www.byui.edu/images/agriculture-life-sciences/flower.jpg" />
      
      <div id="crop">
          <img id="overlay" src="http://www.byui.edu/images/agriculture-life-sciences/flower.jpg" />
      </div>
      

      CSS

      #crop {
          overflow: hidden;
      
          position: absolute;
          left: 100px;
          top: 100px;
      
          width: 450px;
          height: 150px;
      }
      
      #overlay {
          -webkit-filter:blur(4px);
          filter:blur(4px);
      
          width: 450px;
      }
      
      #source {
          height: 300px;
          width: auto;
          position: absolute;
          left: 100px;
          top: 100px;
      }
      

      我知道 CSS 可以简化,您可能应该去掉 id。这里的想法是使用 div 作为裁剪容器,然后在图像的副本上应用模糊。 Fiddle

      要在 Firefox 中使用此功能,您必须使用 SVG hack

      【讨论】:

      • @chipChocolate.py 我猜你需要在这种情况下使用 SVG hack,demosthenes.info/blog/534/Crossbrowser-Image-Blur
      • 请注意,对于 CSS 过滤器,您可以省略 -webkit- 以外的供应商前缀,因为这些浏览器中没有实现它。最好将标准声明放在最后 - 在所有前缀版本之后。
      • @HashemQolami 完成。谢谢。
      • FF 没问题,现在(54.0.1(32 位))。刚刚好!发送!
      【解决方案7】:

      #bg, #search-bg {
        background-image: url('https://images.pexels.com/photos/719609/pexels-photo-719609.jpeg?w=940&h=650&auto=compress&cs=tinysrgb');
        background-repeat: no-repeat;
        background-size: 1080px auto;
      }
      
      #bg {
        background-position: center top;
        padding: 70px 90px 120px 90px;
      }
      
      #search-container {
        position: relative;
      }
      
      #search-bg {
        /* Absolutely position it, but stretch it to all four corners, then put it just behind #search's z-index */
        position: absolute;
        top: 0px;
        right: 0px;
        bottom: 0px;
        left: 0px;
        z-index: 99;
      
        /* Pull the background 70px higher to the same place as #bg's */
        background-position: center -70px;
      
        -webkit-filter: blur(10px);
        filter: url('/media/blur.svg#blur');
        filter: blur(10px);
      }
      
      #search {
        /* Put this on top of the blurred layer */
        position: relative;
        z-index: 100;
        padding: 20px;
        background: rgb(34,34,34); /* for IE */
        background: rgba(34,34,34,0.75);
      }
      
      @media (max-width: 600px ) {
        #bg { padding: 10px; }
        #search-bg { background-position: center -10px; }
      }
      
      #search h2, #search h5, #search h5 a { text-align: center; color: #fefefe; font-weight: normal; }
      #search h2 { margin-bottom: 50px }
      #search h5 { margin-top: 70px }
      <div id="bg">
        <div id="search-container">
          <div id="search-bg"></div>
          <div id="search">
            <h2>Awesome</h2>
            <h5><a href="#">How it works »</a></h5>
          </div>
        </div>
      </div>

      【讨论】:

        【解决方案8】:

        这将对内容进行模糊覆盖:

        .blur {
          display: block;
          bottom: 0;
          left: 0;
          position: fixed;
          right: 0;
          top: 0;
          -webkit-backdrop-filter: blur(15px);
          backdrop-filter: blur(15px);
          background-color: rgba(0, 0, 0, 0.5);
        }
        

        【讨论】:

          【解决方案9】:

          来自backdrop-filter

          ...对区域应用图形效果,例如模糊或颜色偏移 一个元素的后面。因为它适用于背后的一切 元素,要查看效果,您必须制作元素或其背景 至少部分透明。

          【讨论】:

            【解决方案10】:

            我想出了这个解决方案。

            Click to view image of blurry effect

            这是一种技巧,它使用绝对定位的子 div,将其背景图像设置为与父 div 相同,然后将 background-attachment:fixed CSS 属性与相同的 background 属性一起使用父元素。

            然后你在子 div 上应用filter:blur(10px)(或任何值)。

            *{
                margin:0;
                padding:0;
                box-sizing: border-box;
            }
            .background{
                position: relative;
                width:100%;
                height:100vh;
                background-image:url('https://images.unsplash.com/photo-1547937414-009abc449011?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');
                background-size:cover;
                background-position: center;
                background-repeat:no-repeat;
            }
            
            .blur{
                position: absolute;
                top:0;
                left:0;
                width:50%;
                height:100%;
                background-image:url('https://images.unsplash.com/photo-1547937414-009abc449011?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');
                background-position: center;
                background-repeat: no-repeat;
                background-attachment: fixed;
                background-size:cover;
                filter:blur(10px);
                transition:filter .5s ease;
                backface-visibility: hidden;
            }
            
            .background:hover .blur{
                filter:blur(0);
            }
            .text{
                display: inline-block;
                font-family: sans-serif;
                color:white;
                font-weight: 600;
                text-align: center;
                position: relative;
                left:25%;
                top:50%;
                transform:translate(-50%,-50%);
            }
            <head>
                <title>Blurry Effect</title>
            </head>
            <body>
                <div class="background">
                    <div class="blur"></div>
                    <h1 class="text">This is the <br>blurry side</h1>
                </div>
            </body>

            view on codepen

            【讨论】:

              【解决方案11】:

              这是一个适用于固定背景的解决方案,如果您有一个固定的背景并且您有一些叠加的元素并且您需要为它们提供模糊的背景,则此解决方案有效:

              图片我们有这个简单的 HTML:

              <body> <!-- or any wrapper -->
                 <div class="content">Some Texts</div>
              </body>
              

              &lt;body&gt; 或包装元素的固定背景:

              body {
                background-image: url(http://placeimg.com/640/360/any);
                background-size: cover;
                background-repeat: no-repeat;
                background-attachment: fixed;
              }
              

              例如,这里我们有一个带有白色透明背景的叠加元素:

              .content {
                background-color: rgba(255, 255, 255, 0.3);
                position: relative;
              }
              

              现在我们也需要为覆盖元素使用与包装器完全相同的背景图像,我将其用作:before psuedo-class:

              .content:before {
                content: '';
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                z-index: -1;
                filter: blur(5px);
                background-image: url(http://placeimg.com/640/360/any);
                background-size: cover;
                background-repeat: no-repeat;
                background-attachment: fixed;
              }
              

              由于固定背景在包装元素和覆盖元素中的工作方式相同,因此我们在覆盖元素的滚动位置具有完全相同的背景,我们可以简单地对其进行模糊处理。 这是一个工作小提琴,在 Firefox、Chrome、Opera 和 Edge 中测试:https://jsfiddle.net/0vL2rc4d/

              注意: 在 firefox 中有一个 bug 会在滚动时使屏幕闪烁,并且有固定的模糊背景。如果有任何解决方法,请告诉我

              【讨论】:

                猜你喜欢
                • 2022-07-07
                • 2015-12-05
                • 2012-07-21
                • 2019-04-16
                • 2021-11-12
                • 2013-06-10
                • 1970-01-01
                • 1970-01-01
                • 2011-06-25
                相关资源
                最近更新 更多