[css] How to apply a CSS filter to a background image

I have a JPEG file that I'm using as a background image for a search page, and I'm using CSS to set it because I'm working within Backbone.js contexts:

background-image: url("whatever.jpg");

I want to apply a CSS 3 blur filter only to the background, but I'm not sure how to style just that one element. If I try:

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

just underneath background-image in my CSS, it styles the whole page, rather than just the background. Is there a way to select just the image and apply the filter to that? Alternatively, is there a way to just turn the blur off for every other element on the page?

This question is related to css background-image css-filters

The answer is


Now this become even simpler and more flexible by using CSS GRID.You just have to overlap the blured background(imgbg) with the text(h2)

<div class="container">
 <div class="imgbg"></div>
 <h2>
  Lorem ipsum dolor sit amet consectetur, adipisicing elit. Facilis enim
  aut rerum mollitia quas voluptas delectus facere magni cum unde?:)
 </h2>
</div>

and the css:

.container {
        display: grid;
        width: 30em;
      }

.imgbg {
        background: url(bg3.jpg) no-repeat center;
        background-size: cover;
        grid-column: 1/-1;
        grid-row: 1/-1;
        filter: blur(4px);
      }


     .container h2 {
        text-transform: uppercase;
        grid-column: 1/-1;
        grid-row: 1/-1;
        z-index: 2;
      }

Please check the below code:-

_x000D_
_x000D_
.backgroundImageCVR{_x000D_
 position:relative;_x000D_
 padding:15px;_x000D_
}_x000D_
.background-image{_x000D_
 position:absolute;_x000D_
 left:0;_x000D_
 right:0;_x000D_
 top:0;_x000D_
 bottom:0;_x000D_
 background:url('http://www.planwallpaper.com/static/images/colorful-triangles-background_yB0qTG6.jpg');_x000D_
 background-size:cover;_x000D_
 z-index:1;_x000D_
 -webkit-filter: blur(10px);_x000D_
  -moz-filter: blur(10px);_x000D_
  -o-filter: blur(10px);_x000D_
  -ms-filter: blur(10px);_x000D_
  filter: blur(10px); _x000D_
}_x000D_
.content{_x000D_
 position:relative;_x000D_
 z-index:2;_x000D_
 color:#fff;_x000D_
}
_x000D_
<div class="backgroundImageCVR">_x000D_
    <div class="background-image"></div>_x000D_
    <div class="content">_x000D_
      <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>_x000D_
      <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>_x000D_
    </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


You can create a div over the image you want to apply the filter and use backdrop-filter to its CSS class. Check out this link


The following is a simple solution for modern browsers in pure CSS with a 'before' pseudo element, like the solution from Matthew Wilcoxson.

To avoid the need of accessing the pseudo element for changing the image and other attributes in JavaScript, simply use inherit as the value and access them via the parent element (here body).

body::before {
    content: ""; /* Important */
    z-index: -1; /* Important */
    position: inherit;
    left: inherit;
    top: inherit;
    width: inherit;
    height: inherit;
    background-image: inherit;
    background-size: cover;
    filter: blur(8px);
}

body {
  background-image: url("xyz.jpg");
  background-size: 0 0;  /* Image should not be drawn here */
  width: 100%;
  height: 100%;
  position: fixed; /* Or absolute for scrollable backgrounds */
}

All you actually need is "filter":

blur(«WhatEverYouWantInPixels»);"

_x000D_
_x000D_
body {_x000D_
    color: #fff;_x000D_
    font-family: Helvetica, Arial, sans-serif;_x000D_
}_x000D_
_x000D_
#background {_x000D_
    background-image: url('https://cdn2.geckoandfly.com/wp-content/uploads/2018/03/ios-11-3840x2160-4k-5k-beach-ocean-13655.jpg');_x000D_
    background-repeat: no-repeat;_x000D_
    background-size: cover;_x000D_
    width: 100vw;_x000D_
    height: 100vh;_x000D_
    overflow: hidden;_x000D_
    position: absolute;_x000D_
    top: 0;_x000D_
    left: 0;_x000D_
    z-index: -1;_x000D_
_x000D_
    /* START */_x000D_
    /* START */_x000D_
    /* START */_x000D_
    /* START */_x000D_
_x000D_
    /* You can adjust the blur-radius as you'd like */_x000D_
    filter: blur(3px);_x000D_
}
_x000D_
<div id="background"></div>_x000D_
_x000D_
<p id="randomContent">Lorem Ipsum</p>
_x000D_
_x000D_
_x000D_


I didn't write this, but I noticed there was a polyfill for the partially supported backdrop-filter using the CSS SASS compiler, so if you have a compilation pipeline it can be achieved nicely (it also uses TypeScript):

https://codepen.io/mixal_bl4/pen/EwPMWo


In the .content tab in CSS change it to position:absolute. Otherwise, the page rendered won't be scrollable.


This answer is for a Material Design horizontal card layout with dynamic height and an image.

To prevent distortion of the image due to the dynamic height of the card, you could use a background placeholder image with blur to adjust for changes in height.

 

Explanation

  • The card is contained in a <div> with class wrapper, which is a flexbox.
  • The card is made up of two elements, an image which is also a link, and content.
  • The link <a>, class link, is positioned relative.
  • The link consists of two sub-elements, a placeholder <div> class blur and an <img> class pic which is the clear image.
  • Both are positioned absolute and have width: 100%, but class pic has a higher stack order, i.e., z-index: 2, which places it above the placeholder.
  • The background image for the blurred placeholder is set via inline style in the HTML.

 

Code

_x000D_
_x000D_
.wrapper {_x000D_
  display: flex;_x000D_
  width: 100%;_x000D_
  border: 1px solid rgba(0, 0, 0, 0.16);_x000D_
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.16), 0 1px 1px rgba(0, 0, 0, 0.23);_x000D_
  background-color: #fff;_x000D_
  margin: 1rem auto;_x000D_
  height: auto;_x000D_
}_x000D_
_x000D_
.wrapper:hover {_x000D_
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);_x000D_
}_x000D_
_x000D_
.link {_x000D_
  display: block;_x000D_
  width: 200px;_x000D_
  height: auto;_x000D_
  overflow: hidden;_x000D_
  position: relative;_x000D_
  border-right: 2px solid #ddd;_x000D_
}_x000D_
_x000D_
.blur {_x000D_
  position: absolute;_x000D_
  top: 0;_x000D_
  left: 0;_x000D_
  right: 0;_x000D_
  bottom: 0;_x000D_
  margin: auto;_x000D_
  width: 100%;_x000D_
  height: 100%;_x000D_
  filter: blur(5px);_x000D_
  -webkit-filter: blur(5px);_x000D_
  -moz-filter: blur(5px);_x000D_
  -o-filter: blur(5px);_x000D_
  -ms-filter: blur(5px);_x000D_
}_x000D_
_x000D_
.pic {_x000D_
  width: calc(100% - 20px);_x000D_
  max-width: 100%;_x000D_
  height: auto;_x000D_
  margin: auto;_x000D_
  position: absolute;_x000D_
  top: 0;_x000D_
  left: 0;_x000D_
  right: 0;_x000D_
  bottom: 0;_x000D_
  z-index: 2;_x000D_
}_x000D_
_x000D_
.pic:hover {_x000D_
  transition: all 0.2s ease-out;_x000D_
  transform: scale(1.1);_x000D_
  text-decoration: none;_x000D_
  border: none;_x000D_
}_x000D_
_x000D_
.content {_x000D_
  display: flex;_x000D_
  flex-direction: column;_x000D_
  width: 100%;_x000D_
  max-width: 100%;_x000D_
  padding: 20px;_x000D_
  overflow-x: hidden;_x000D_
}_x000D_
_x000D_
.text {_x000D_
  margin: 0;_x000D_
}
_x000D_
<div class="wrapper">_x000D_
  <a href="#" class="link">_x000D_
    <div class="blur" style="background: url('http://www.planwallpaper.com/static/assets/img/header.jpg') 50% 50% / cover;"></div>_x000D_
    <img src="http://www.planwallpaper.com/static/assets/img/header.jpg" alt="Title" class="pic" />_x000D_
  </a>_x000D_
_x000D_
  <div class="content">_x000D_
    <p class="text">Agendum dicendo memores du gi ad. Perciperem occasionem ei ac im ac designabam. Ista rom sibi vul apud tam. Notaverim to extendere expendere concilium ab. Aliae cogor tales fas modus parum sap nullo. Voluntate ingressus infirmari ex mentemque ac manifeste_x000D_
      eo. Ac gnum ei utor sive se. Nec curant contra seriem amisit res gaudet adsunt. </p>_x000D_
  </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


_x000D_
_x000D_
div {_x000D_
    background: inherit;_x000D_
    width: 250px;_x000D_
    height: 350px;_x000D_
    position: absolute;_x000D_
    overflow: hidden;  /* Adding overflow hidden */_x000D_
}_x000D_
_x000D_
div:before {_x000D_
    content: ‘’;_x000D_
    width: 300px;_x000D_
    height: 400px;_x000D_
    background: inherit;_x000D_
    position: absolute;_x000D_
    left: -25px;  /* Giving minus -25px left position */_x000D_
    right: 0;_x000D_
    top: -25px;   /* Giving minus -25px top position */_x000D_
    bottom: 0;_x000D_
    box-shadow: inset 0 0 0 200px rgba(255, 255, 255, 0.3);_x000D_
    filter: blur(10px);_x000D_
}
_x000D_
_x000D_
_x000D_


pen

Abolishing the need for an extra element, along with making the content fit within the document flow rather than being fixed/absolute like other solutions.

Achieved using

_x000D_
_x000D_
.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);
}
_x000D_
<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>
_x000D_
_x000D_
_x000D_

EDIT If you are interested in removing the white borders at the edges, use a width and height of 110% and a left and top of -5%. This will enlarge your backgrounds a tad - but there should be no solid colour bleeding in from the edges. Thanks Chad Fawcett for the suggestion.

_x000D_
_x000D_
.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);
}
_x000D_
<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>
_x000D_
_x000D_
_x000D_


$fondo: url(/grid/assets/img/backimage.png);    
{ padding: 0; margin: 0; } 
body { 
    ::before{
        content:"" ; height: 1008px; width: 100%; display: flex; position: absolute; 
        background-image: $fondo ; background-repeat: no-repeat ; background-position: 
        center; background-size: cover; filter: blur(1.6rem);
    }
}

You need to re-structure your HTML in order to do this. You have to blur the whole element in order to blur the background. So if you want to blur only the background, it has to be its own element.


Although all the solutions mentioned are very clever, all seemed to have minor issues or potential knock on effects with other elements on the page when I tried them.

In the end to save time I simply went back to my old solution: I used Paint.NET and went to Effects, Gaussian Blur with a radius 5 to 10 pixels and just saved that as the page image. :-)

HTML:

<body class="mainbody">
</body

CSS:

body.mainbody
{
    background: url('../images/myphoto.blurred.png');
    -moz-background-size: cover;
    -webkit-background-size: cover;
    background-size: cover;
    background-position: top center !important;
    background-repeat: no-repeat !important;
    background-attachment: fixed;
}

EDIT:

I finally got it working, but the solution is by no means straightforward! See here:


Of course, this is not a CSS-solution, but you can use the CDN Proton with filter:

body {
    background: url('https://i0.wp.com/IMAGEURL?w=600&filter=blurgaussian&smooth=1');
}

It is from https://developer.wordpress.com/docs/photon/api/#filter


If you want to content to be scrollable, set the position of the content to absolute:

content {
   position: absolute;
   ...
}

I don't know if this was just for me, but if not that's the fix!

Also since the background is fixed, it means you have a "parallax" effect! So now, not only did this person teach you how to make a blurry background, but it is also a parallax background effect!


Without using the pseudo-class from

_x000D_
_x000D_
body{_x000D_
    background:#cecece;_x000D_
    font-family: "Scope One", serif;_x000D_
    font-size: 12px;_x000D_
    color:black;_x000D_
    margin:0 auto;_x000D_
    height:100%;_x000D_
    width:100%;_x000D_
    background-image: _x000D_
       linear-gradient(to bottom, rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.81)), _x000D_
        url(https://i.imgur.com/2rRMQh7.jpg);_x000D_
       -webkit-background-size: cover;_x000D_
        -moz-background-size: cover;_x000D_
        -o-background-size: cover;_x000D_
        background-size: cover; _x000D_
}
_x000D_
<body></body>
_x000D_
_x000D_
_x000D_


As stated in other answers this can be achieved with:

  • A copy of the blurred image as the background.
  • A pseudo element that can be filtered then positioned behind the content.

You can also use backdrop-filter

There is a supported property called backdrop-filter, and it is currently supported in Chrome 76, Edge, Safari, and iOS Safari (see caniuse.com for statistics).

From Mozilla devdocs:

The backdrop-filter property provides for effects like blurring or color shifting the area behind an element, which can then be seen through that element by adjusting the element's transparency/opacity.

See caniuse.com for usage statistics.

You would use it like so. If you do not want content inside to be blurred use the utility class .u-non-blurred

_x000D_
_x000D_
.background-filter::after {
  -webkit-backdrop-filter: blur(5px); /* Use for Safari 9+, Edge 17+ (not a mistake) and iOS Safari 9.2+ */
  backdrop-filter: blur(5px); /* Supported in Chrome 76 */

  content: "";
  display: block;
  position: absolute;
  width: 100%; height: 100%;
  top: 0;
}

.background-filter {
  position: relative;
}

.background {
  background-image: url('https://upload.wikimedia.org/wikipedia/en/6/62/Kermit_the_Frog.jpg');
  width: 200px;
  height: 200px;
}

/* Use for content that should not be blurred */
.u-non-blurred {
  position: relative;
  z-index: 1;
}
_x000D_
<div class="background background-filter"></div>
<div class="background background-filter">
  <h1 class="u-non-blurred">Kermit D. Frog</h1>
</div>
_x000D_
_x000D_
_x000D_

Update (12/06/2019): Chromium will ship with backdrop-filter enabled by default in version 76 which is due out 30/07/2019.

Update (01/06/2019): The Mozzilla Firefox team has announced it will start working on implementing this soon.

Update (21/05/2019): Chromium just announced backdrop-filter is available in chrome canary without enabling "Enable Experimental Web Platform Features" flag. This means backdrop-filter is very close to being implemented on all chrome platforms.