How to apply CSS filters to background images
P粉675258598
2023-08-23 14:06:46
<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>
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
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.
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
andleft:0;right:0;
. The difference shown comes from setting differentz-index
values for the elements.