How to use CSS filter effects on background images
P粉022285768
P粉022285768 2023-08-20 13:28:36
0
2
403

I have a JPEG file that I'm using as a background image for my search page, and I'm setting it up using CSS because I'm working in a Backbone.js context:

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

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

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

Place the above code in my CSS under background-image and it will style the entire page, not just the background. Is there a way to select just an image and apply a filter to that image? Alternatively, is there a way to just turn off the blur effect for other elements on the page?

P粉022285768
P粉022285768

reply all (2)
P粉491421413

pen

Does away with the need for extra elements, allowing content to fit into the document flow rather than being fixed/absolute like other solutions.

Use the following methods to achieve

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

EditIf you want to remove the white border at the edges, use110%for width and height and-5%for left and top. This will slightly enlarge your background image, but won't have 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); }
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.
    P粉270842688

    View thisNotebook.

    You will need to use two different containers, one for the background image and another for the content.

    In the example, I created two containers,.background-imageand.content.

    They all useposition: fixedandleft: 0; right: 0;for positioning. The difference in their display comes from the differentz-indexvalues set 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; }

    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.

    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.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!