CSS blur and retain sharp edges using absolute div

前端 未结 3 2371
忘掉有多难
忘掉有多难 2021-02-19 05:58

This works just fine if img is not set to absolute:

div img {
    filter: blur(5px);
        -webkit-filter: blur(5px);
        -moz-filter: blur(5px);
        -         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-19 06:05

    An alternative to the answers I've been seeing for this, that I think is really clever, is using an svg to blur the img. It's described really well in this codepen https://codepen.io/johndjameson/full/xVjgPy/ so I'll just copy and paste that over here. Hopefully it'll be much more accessible on this thread.

    To sum it up. You make an invisible svg element like this

     
      
        
        
        
      
    
    

    And then use CSS to blur by linking to the invisible svg element

    .svgBlur { filter: url("#sharpBlur"); }
    

    Finally you just add svgBlur to the img you want blurred

    
    

    And that's it! Worked really well for me.

    .cssBlur {
      -webkit-filter: blur(3px);
      filter: blur(3px);
    }
    
    .svgBlur {
      -webkit-filter: url("#sharpBlur");
      filter: url("#sharpBlur");
    }
    
    .hideSvgSoThatItSupportsFirefox {
      border: 0;
      clip: rect(0 0 0 0);
      height: 1px;
      margin: -1px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1px;
    }
    
    *,
     ::before,
     ::after {
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
    }
    
    html {
      padding: 40px;
      line-height: 1.4;
      margin-left: auto;
      margin-right: auto;
      max-width: 840px;
    }
    
    svg {
      width: 0;
      height: 0;
      overflow: hidden;
      visibility: hidden;
    }
    
    img {
      height: auto;
      max-width: 100%;
      vertical-align: middle;
    }
    
    h1,
    h2,
    h3,
    h4,
    h5,
    h6,
    p,
    pre {
      margin-top: 0;
      margin-bottom: 0;
    }
    
    h1,
    h2,
    h3 {
      line-height: 1.2;
    }
    
    h1 {
      margin-bottom: 40px;
    }
    
    h2 {
      margin-bottom: 20px;
    }
    
    p {
      margin-bottom: 20px;
    }
    
    pre {
      margin-bottom: 20px;
      overflow: auto;
    }
    
    .emoji {
      font-size: 40px;
      line-height: 1;
    }
    
    .grid {
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -ms-flex-wrap: wrap;
      flex-wrap: wrap;
      margin-left: -40px;
    }
    
    .grid-box {
      padding-left: 40px;
      width: 100%;
    }
    
    @media screen and (min-width: 600px) {
      .grid-box--1of2 {
        width: 50%;
      }
    }
    
    .mbf {
      margin-bottom: 0;
    }
    
    .mbm {
      margin-bottom: 40px;
    }
    
    .mbs {
      margin-bottom: 20px;
    }
    
    @media screen and (min-width: 600px) {
      .mbf_m {
        margin-bottom: 0;
      }
      .mbm_m {
        margin-bottom: 40px;
      }
    }
    
      
        
        
        
      
    
    

    Blurred Image with Sharp Edges

    Original image

    Let’s blur this image in the browser. There are two types of filters we can use: CSS and SVG.

    If you want to keep the orginal’s sharp edges, you’re going to need SVG.

提交回复
热议问题