Background blur with CSS

前端 未结 7 2150
忘了有多久
忘了有多久 2020-11-27 13:34

I want an Vista/7-aero-glass-style effect on a popup on my site, and it needs to be dynamic. I\'m fine with this not being a cross-browser effect as long as

相关标签:
7条回答
  • 2020-11-27 13:42

    You can use a pseudo-element to position as the background of the content with the same image as the background, but blurred with the new CSS3 filter.

    You can see it in action here: http://codepen.io/jiserra/pen/JzKpx

    I made that for customizing a select, but I added the blur background effect.

    0 讨论(0)
  • 2020-11-27 13:55

    In recent versions of major browsers you can use backdrop-filter property.

    HTML

    <div>backdrop blur</div>
    

    CSS

    div {
        -webkit-backdrop-filter: blur(10px);
        backdrop-filter: blur(10px);
    }
    

    or if you need different background color for browsers without support:

    div {
        background-color: rgba(255, 255, 255, 0.9);
    }
    
    @supports (-webkit-backdrop-filter: none) or (backdrop-filter: none) {
        div {
            -webkit-backdrop-filter: blur(10px);
            backdrop-filter: blur(10px);
            background-color: rgba(255, 255, 255, 0.5);  
        }
    }
    

    Demo: JSFiddle

    Docs: Mozilla Developer: backdrop-filter

    Is it for me?: CanIUse

    0 讨论(0)
  • 2020-11-27 13:56

    There is a simple and very common technique by using 2 background images: a crisp and a blurry one. You set the crisp image as a background for the body and the blurry one as a background image for your container. The blurry image must be set to fixed positioning and the alignment is 100% perfect. I used it before and it works.

    body {
        background: url(yourCrispImage.jpg) no-repeat;
    }
    
    #container {
        background: url(yourBlurryImage.jpg) no-repeat fixed;
    }
    

    You can see a working example at the following fiddle: http://jsfiddle.net/jTUjT/5/. Try to resize the browser and see that the alignment never fails.


    If only CSS element() was supported by other browsers other than Mozilla's -moz-element() you could create great effects. See this demo with Mozilla.

    0 讨论(0)
  • 2020-11-27 14:01

    In which way do you want it dynamic? If you want the popup to successfully map to the background, you need to create two backgrounds. It requires both the use of element() or -moz-element() and a filter (for Firefox, use a SVG filter like filter: url(#svgBlur) since Firefox does not support -moz-filter: blur() as yet?). It only works in Firefox at the time of writing.

    See demo here.

    I still need to create a simple demo to show how it is done. You're welcome to view the source.

    0 讨论(0)
  • 2020-11-27 14:02

    You could make it through iframes... I made something, but my only problem for now is syncing those divs to scroll simultaneous... its terrible way, because its like you load 2 websites, but the only way I found... you could also work with divs and overflow I guess...

    0 讨论(0)
  • 2020-11-27 14:05

    Use an empty element sized for the content as the background, and position the content over the blurred element.

    #dialog_base{
      background:white;
      background:rgba(255,255,255,0.8);
    
      position: absolute;
      top: 40%;
      left: 50%;
      z-index: 50;
      margin-left: -200px;
      height: 200px;
      width: 400px;
    
      filter:blur(4px);
      -o-filter:blur(4px);
      -ms-filter:blur(4px);
      -moz-filter:blur(4px);
      -webkit-filter:blur(4px);
    }
    
    #dialog_content{
      background: transparent;
      position: absolute;
      top: 40%;
      left: 50%;
      margin-left -200px;
      overflow: hidden;
      z-index: 51;
    }
    

    The background element can be inside of the content element, but not the other way around.

    <div id='dialog_base'></div>
    <div id='dialog_content'>
        Some Content
        <!-- Alternatively with z-index: <div id='dialog_base'></div> -->
    </div>
    

    This is not easy if the content is not always consistently sized, but it works.

    0 讨论(0)
提交回复
热议问题