Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 273
fox Avatar asked Nov 18 '13 03:11

fox


People also ask

How do you add a background filter in CSS?

* To get this to work, open about:config and set thelayout. css. backdrop-filter. enabled to true, and also the gfx.

How do you add a background image to grayscale in CSS?

Greyscale Hover: -webkit-filter: grayscale(100%); Greyscale "Hover-out": -webkit-filter: grayscale(0%);

How do you make a background image black in CSS?

Use the background-blend-mode Property to Darken Background Image in CSS. We can use the background-blend-mode property to darken the background image in CSS. The property sets the blending mode of the background.


2 Answers

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

.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 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.

.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>
like image 37
Necrone Avatar answered Sep 20 '22 17:09

Necrone


Check out this pen.

You will have to use two different containers, one for the background image and the other for your content.

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

Both of them are placed with position: fixed and left: 0; right: 0;. The difference in displaying them comes from the z-index values which have been set differently 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>

Apologies for the Lorem Ipsum text.

Update

Thanks to Matthew Wilcoxson for a better implementation using .content::before https://codepen.io/akademy/pen/FlkzB

like image 118
Aniket Avatar answered Sep 23 '22 17:09

Aniket