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 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
<div>
with class wrapper, which is a flexbox.<a>
, class link, is positioned relative.<div>
class blur and an<img>
class pic which is the clear image.width: 100%
, but class pic has a higher stack order, i.e.,z-index: 2
, which places it above the placeholder.Code
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:
CSS:
EDIT:
I finally got it working, but the solution is by no means straightforward! See here:
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 java script, simple use inherit as value and access them via the parent element (here body).
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.
Please check the below code:-