Home  >  Q&A  >  body text

How to apply CSS filters to background images

<p>I have a JPEG file that I use as a background image for my search page, and I'm using CSS to set it because I'm working in a Backbone.js context: </p> <pre class="brush:php;toolbar:false;">background-image: url("whatever.jpg");</pre> <p>I want to<em>apply a CSS 3 blur filter only</em>to the background, but I'm not sure how to style just that element. If I try: </p> <pre class="brush:php;toolbar:false;">-webkit-filter: blur(5px); -moz-filter: blur(5px); -o-filter: blur(5px); -ms-filter: blur(5px); filter: blur(5px);</pre> <p>Right below <code>background-image</code> in my CSS, it styles the entire page, not just the background. Is there a way to just select the image and apply a filter to it? Alternatively, is there a way to turn off the blur for all other elements on the page? </p>
P粉675258598P粉675258598393 days ago438

reply all(2)I'll reply

  • P粉521697419

    P粉5216974192023-08-24 12:52:20

    Pen

    Eliminates the need for extra elements while fitting the content into the document flow rather than being fixed/absolute like other solutions.

    Use implementation

    .content {
      /* this is needed or the background will be offset by a few pixels at the top */
      overflow: auto;
      position: relative;
    }
    
    .content::before {
      content: "";
      position: fixed;
      left: 0;
      right: 0;
      z-index: -1;
    
      display: block;
      background-image: url('https://i.imgur.com/lL6tQfy.png');
      background-size:cover;
      width: 100%;
      height: 100%;
    
      -webkit-filter: blur(5px);
      -moz-filter: blur(5px);
      -o-filter: blur(5px);
      -ms-filter: blur(5px);
      filter: blur(5px);
    }
    <div class="content">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>

    EDIT If you are interested in removing the white border on the edges, use 110% for width and height and -5 for left and top %. This will slightly enlarge your background - but there shouldn't be any solid color bleeding through the edges. Thanks to Chad Fawcett for the suggestion.

    .content {
      /* this is needed or the background will be offset by a few pixels at the top */
      overflow: auto;
      position: relative;
    }
    
    .content::before {
      content: "";
      position: fixed;
      top: -5%;
      left: -5%;
      right: -5%;
      z-index: -1;
    
      display: block;
      background-image: url('https://i.imgur.com/lL6tQfy.png');
      background-size:cover;
      width: 110%;
      height: 110%;
    
      -webkit-filter: blur(5px);
      -moz-filter: blur(5px);
      -o-filter: blur(5px);
      -ms-filter: blur(5px);
      filter: blur(5px);
    }
    <div class="content">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>

    reply
    0
  • P粉316423089

    P粉3164230892023-08-24 10:25:04

    Look at this pen.

    You must use two different containers, one for the background image and another for your content.

    In the example, I created two containers: .background-image and .content.

    Both are placed as position:fixed and left:0;right:0;. The difference shown comes from setting different z-index values ​​for the elements.

    .background-image {
      position: fixed;
      left: 0;
      right: 0;
      z-index: 1;
      display: block;
      background-image: url('https://i.imgur.com/lL6tQfy.png');
      width: 1200px;
      height: 800px;
      -webkit-filter: blur(5px);
      -moz-filter: blur(5px);
      -o-filter: blur(5px);
      -ms-filter: blur(5px);
      filter: blur(5px);
    }
    
    .content {
      position: fixed;
      left: 0;
      right: 0;
      z-index: 9999;
      margin-left: 20px;
      margin-right: 20px;
    }
    <div class="background-image"></div>
    <div class="content">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis aliquam erat in ante malesuada, facilisis semper nulla semper. Phasellus sapien neque, faucibus in malesuada quis, lacinia et libero. Sed sed turpis tellus. Etiam ac aliquam tortor, eleifend
        rhoncus metus. Ut turpis massa, sollicitudin sit amet molestie a, posuere sit amet nisl. Mauris tincidunt cursus posuere. Nam commodo libero quis lacus sodales, nec feugiat ante posuere. Donec pulvinar auctor commodo. Donec egestas diam ut mi adipiscing,
        quis lacinia mauris condimentum. Quisque quis odio venenatis, venenatis nisi a, vehicula ipsum. Etiam at nisl eu felis vulputate porta.</p>
      <p>Fusce ut placerat eros. Aliquam consequat in augue sed convallis. Donec orci urna, tincidunt vel dui at, elementum semper dolor. Donec tincidunt risus sed magna dictum, quis luctus metus volutpat. Donec accumsan et nunc vulputate accumsan. Vestibulum
        tempor, erat in mattis fringilla, elit urna ornare nunc, vel pretium elit sem quis orci. Vivamus condimentum dictum tempor. Nam at est ante. Sed lobortis et lorem in sagittis. In suscipit in est et vehicula.</p>
    </div>

    reply
    0
  • Cancelreply