Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defined Edges With CSS3 Filter Blur

Tags:

css

I am blurring some images with this code

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

The edges of the image get blurred too though. Is it possible to blur the image, while keeping the edges defined? Like an inset blur or something?

like image 624
colmtuite Avatar asked Sep 01 '12 02:09

colmtuite


People also ask

How do I blur edges in CSS?

Blurring the edges of a photo in CSS is pretty straightforward, with one gotcha. To blur a photo we need to use box-shadow in a way where the shadow "eats" the image. For this effect to work, the blur must be the same color as the surrounding background, and inset must be used.

How do I blur an image without border in CSS?

Use backdrop-filter instead! It blurs just like filter but without any edges, and without any compromises like resizing or scaling the image.

Which CSS attribute can be specified to apply a blur effect to an element?

The filter property defines visual effects (like blur and saturation) to an element (often <img>).

Which CSS property can be used with Blur 10px to blur an image?

Syntax. filter: blur(px); To apply a blur effect to the background image, with the blur function use the z-index property to set the stack order of the element, set the width and height 100% to have a full background image.


2 Answers

You could put it in a <div> with overflow: hidden; and set the <img> to margin: -5px -10px -10px -5px;.

Demo: jsFiddle

Output

CSS

img {     filter: blur(5px);         -webkit-filter: blur(5px);         -moz-filter: blur(5px);         -o-filter: blur(5px);         -ms-filter: blur(5px);     margin: -5px -10px -10px -5px; }  div {     overflow: hidden; } ​ 

HTML

<div><img src="http://placekitten.com/300" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​</div>​​​​​​​​​​​​ 
like image 137
ThinkingStiff Avatar answered Sep 23 '22 05:09

ThinkingStiff


I was able to make this work with the

transform: scale(1.03);

Property applied on the image. For some reason, on Chrome, the other solutions provided wouldn't work if there was any relatively positioned parent element.

Check http://jsfiddle.net/ud5ya7jt/

This way the image will be slightly zoomed in by 3% and the edges will be cropped which shouldn't be a problem on a blurred image anyway. It worked well in my case because I was using a high res image as a background. Good luck!

like image 20
João Josézinho Avatar answered Sep 22 '22 05:09

João Josézinho