Blur Img's & Div's in HTML using CSS - javascript

Is it possible to apply a blur to an HTML element(div & img)?
I am developing solely for the iPad so cross-browser compatibility is not an issue & I can use HTML5 CSS3 techniques.
I know how to blur text but this CSS doesn't blur the actual HTML element or its border:
text-shadow: 0 0 8px #000;
color: transparent;
I googled this but it doesn't blur the image in my browsers:
filter: blur(strength=50);

Webkit has a property called -webkit-filter that allows for the techniques of blurring: -webkit-filter: blur(15px);
http://jsfiddle.net/danielfilho/KxWRA/

You can simply add this to your css, for an image:
In the following example, you'll be using a blur with 5 pixels of radius. And it is extremely important to use all vendor prefixes available, so it works on all browsers with this feature implemented, untile it comes to a "stable" version.
img{
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-ms-filter: blur(5px);
-o-filter: blur(5px);
filter: blur(5px);
}

I think the best way is to layer the same image over itself a few times and test different positioning and opacities on the overlayed ones.
Here's the CSS that I came up with. Keep in mind I'm using the nth-child CSS3 selector (but you don't seem to have an issue with that):
img {
width:300px;
height:300px;
position:absolute;
opacity:0.2;
}
.container {
position:relative;
overflow:hidden;
width:300px;
height:300px;
}
img:nth-child(1) {
opacity:1;
}
img:nth-child(2) {
left:2px;
top:2px;
}
img:nth-child(3) {
left:-2px;
top:-2px;
}
img:nth-child(4) {
left:-1px;
top:-1px;
}
img:nth-child(5) {
left:1px;
top:1px;
}
HTML:
<div class="container">
<img src="...">
<img src="...">
<img src="...">
<img src="...">
<img src="...">
</div>
The result is pretty promising.

CSS does not have the ability to blur, besides techniques with text-shadow and box-shadow. But even with these, borders and images aren't able to be blurred.
This JavaScript library, however, can handle images.
Also, you may find this technique interesting. It's a neat illusion using pre-fabricated blurry images.

I had to pretty thoroughly research this problem not too long ago and came up with an extremely flexible solution, though it may be overkill for some people's needs. I needed not only blurred images, but also a dynamic blur radius, overlay color, and overlay opacity for various kinds of images. I also needed to have the option of just blurring an image in a background with other elements overlayed on top of it. Here's the best cross-browser (and performant) solution I was able to create.
First, I'd have an SVG on hand, uninspiringly called blur.svg. It applies a blur filter and if you look closely, the stdDeviation (which sets the blur radius) is actually set programmatically from a passed in parameter to the URL requesting the asset.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<filter id="blur">
<feGaussianBlur stdDeviation="#{params[:blur]}" />
</filter>
</svg>
Then I had an SCSS mixin that would allow one to add a blur overlay to any wrapper, with a customizable blur radius, overlay color, and overlay opacity.
#mixin background_blurred($blur_radius:4, $overlay_color:white, $overlay_opacity:0.6) {
position: relative;
.background_blurred {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
z-index: 1;
filter: url('blur.svg#blur?blur=#{$blur_radius}');
filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius='#{$blur_radius}');
transform: translateZ(0);
&:after {
content: '';
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
background: $overlay_color;
opacity: $overlay_opacity;
}
}
.foreground {
width: 100%;
height: 100%;
position: relative;
z-index: 2;
}
}
You may be wondering why I included a transform: translateZ(0);. The only effect that has is to force hardware acceleration on the render to keep things performant. You may also be wondering why there are no vendor prefixes. You can look up things like filter on CanIUse if you want, but I used autoprefixer on this project to worry about that stuff for me. And of course, why filter using this SVG, rather than with something like blur(4px)? Wouldn't that be easier? It would, but Firefox (as of writing) only supports the filter property with a URL.
Then you can apply the blur mixin to a wrapper class:
.my_wrapper_class {
#include background_blurred(3, #f9f7f5, 0.7);
}
Notice that for this method, we have to use a class with a custom background set in a style attribute instead of an image tag with a src. You can tweak the background position and override the background size to your liking.
<div class="my_wrapper_class">
<div class="background_blurred" style="background: url('URL OF IMAGE TO BLUR') no-repeat; background-position: 50% 0;"></div>
<div class="foreground">
<p>Stuff that should appear above the blurred background and not be blurred.</p>
</div>
</div>

With CSS3 we can easily adjust an image. But remember this does not change the image. It only displays the adjusted image.
See live demo and complete source code here
http://purpledesign.in/blog/adjust-an-image-using-css3-html5/
See the following code for more details.
To make an image gray:
img {
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
}
To give a sepia look:
img {
-webkit-filter: sepia(100%);
-moz-filter: sepia(100%);
}
To adjust brightness:
img {
-webkit-filter: brightness(50%);
-moz-filter: brightness(50%);
}
To adjust contrast:
img {
-webkit-filter: contrast(200%);
-moz-filter: contrast(200%);
}
To Blur an image:
img {
-webkit-filter: blur(10px);
-moz-filter: blur(10px);
}

Related

css blur effect on a live background [duplicate]

This question already has answers here:
How to use CSS (and JavaScript?) to create a blurred, "frosted" background?
(10 answers)
Closed 5 years ago.
I was looking for a way to blur/glass a background to create some overlays and dialogs. I stumbled upon lots of possible solutions, but none of them fits my requirement.
You can either solve this issue by using two versions of the same image (original + blurred) and then offset the blurred version in the overlay background or you could possibly use some crazy stuff like html2canvas, create a snapshot and basically go for the first solution.
The problem is, that isn't "live" at all. If something changes in the DOM, you don't catch it, especially not if you're just using a blurred version of a single image.
Gecko to the rescue?
Firefox/Gecko introduced a pretty nice css feature called element() a long time ago. It allows you to just clone the face of any element in your live DOM. That comes in pretty handy, to solve my original problem and it looks like this:
Demo: https://codepen.io/anon/pen/prLBpQ (only works in Firefox, unfortunately).
The great thing about element() is, that it is truly live, even if you move other elements within a "target" surface, it reflects instantly on your background.
As awesome as this feature is, it's only available in Firefox for years, so my only question is, if there is any other smart way to create a similar live effect on webkit, which I could not think of at present.
// Js only for drag the articles
$(function() {
$( "article" ).draggable();
});
html {
background: url(https://2.bp.blogspot.com/-LwilPQw9Zc0/Unzm09oXDxI/AAAAAAAAHwo/30a7ZqSp3jE/s1600/blur-static+.jpg) no-repeat 50% fixed;
background-size: cover;
}
body {
width: 100%;
min-height: 100%;
background: inherit;
overflow: hidden;
}
article {
background: inherit;
position: relative;
width: 60%;
margin: 10vh auto 15vh;
border-radius: 15px;
border: 10px solid rgba(255,255,255,.15);
box-shadow: 1px 1px 4px rgba(0,0,0,.3);
z-index: 5;
font-size: 1.4rem;
cursor: move;
}
article:before {
content: '';
position: absolute;
top: 0; left:0; right:0; bottom:0;
background: inherit;
filter: blur(5px);
-webkit-filter: blur(6px);
-moz-filter: blur(6px);
-o-filter: blur(6px);
-ms-filter: blur(6px);
filter: url(#blur);filter:progid:DXImageTransform.Microsoft.Blur(PixelRadius='6');
}
<article>
<h2>Blur effect</h2>
</article>
<svg version="1.1" xmlns='http://www.w3.org/2000/svg'>
<filter id='blur'>
<feGaussianBlur stdDeviation='6' />
</filter>
</svg>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

Setting pseudo element background image using javascript

I am having a div with before selector. I set the background of this div and added CSS blur filter to it. I need to change the background image of this pseudo element dynamically using javascript. When I manually give a location for the background, it works fine and I succeeded in setting the image dynamically by appending the style to the head tag but the image won't resize. I need the image to be stretched to the full width of viewport but it is displayed in its original size only. Here is my CSS and javascript
CSS:
.content-wrapper::before {
position: fixed;
z-index: -1;
display: block;
width: 100%;
height: 100%;
/* background: url(/landing/images/profile.jpg); */
background-size: cover;
-webkit-filter: blur(25px)brightness(.7);
-moz-filter: blur(25px)brightness(.7);
-o-filter: blur(25px)brightness(.7);
-ms-filter: blur(25px)brightness(.7);
filter: blur(25px)brightness(.7);
}
Javascript:
$('head').append('<style>.content-wrapper::before{background:url('+response.data.url+') no-repeat}</style>');
This is the result now:
Update: Solution
Alex's fiddle link in the comments section worked like a charm. I had to change the position:fixed and background to position:absolute and background-image

Blur changing elements behind div

First of all I want to make clear that I already have searched for some solutions. And I also have seen some 'blur issues' here on stackoverflow. But mine is just little different.
I want to blur the part of a slideshow that falls behind a transparent div element. On my testserver I have a preview to make it understandable you can check it here.
The things I have already tried:
- blurjs.com
- CSS only blur wich you can check here
- Google
These two basicly do the same, both the solutions don't support changing backgrounds. Maybe you guys know a solution.
I hope you can help me. Thanks.
The code I have till now:
<div id="blur-overlay"></div>
<div id="slideshow">
<div class="slide active"><img src="http://www.zastavki.com/pictures/originals/2012/Space_Earth_from_space_035859_.jpg" alt="Slideshow Image 1" /></div>
<div class="slide"><img src="http://www.zastavki.com/pictures/originals/2012/Space_Full_Moon_from_the_Earth_035858_.jpg" alt="Slideshow Image 2" /></div>
<div class="slide"><img src="http://i.space.com/images/i/000/005/402/original/hubble-space-bubble-photo-2-100622-02.jpg?1292270748" alt="Slideshow Image 3" /></div>
</div>
and the CSS:
#blur-overlay {
width: 250px;
height: 250px;
border-radius: 100%;
background: rgba(255, 255, 255, 0.5);
position: absolute;
top: 105px;
left: 50%;
margin-left: -125px;
z-index: 100;
}
#slideshow {
position:relative;
height:300px;
width:1000px;
float: left;
overflow: hidden;
}
#slideshow .slide {
position:absolute;
top:0;
left:0;
z-index:8;
opacity:0.0;
}
#slideshow div {
display: none;
}
#slideshow div img {
width: 100%;
float: left;
position: relative;
}
#slideshow div.active {
display: block;
z-index:10;
opacity:1.0;
}
#slideshow div.last-active {
z-index:9;
}
(as far as I'm aware) There is no easy way to make that happen. This is because CSS blurs only apply to specific elements. So you could blur your transparent circle, but it will not affect whatever is behind it.
The only possible solution I can think of would be to have TWO slideshows running at once. The one you already have, and then a second one on top. The second one would have CSS blur applied. Then on the blurred slideshow, you could use CSS to make it a circle with overflow hidden.
EDIT: I misunderstood what was trying to be done. The poster wishes to blur the background behind a div and not the div itself. Only way to do that is to have a part of the image blurred, which is actually also covered in the article. Also refer to the other answer.
Original:
I think this is a symptom of not reading through the articles fully. Here's a code example right from the article(CSS Gaussian blur behind a translucent box):
#search {
filter: blur(10px); /* Doesn't work anywhere yet */
-webkit-filter: blur(10px);
filter: url('blur.svg#blur'); /* for Firefox - http://jordanhollinger.com/media/blur.svg */
}
On looking at your site, I saw this:
#blur-overlay {
filter: blur(15px);
}
The article clearly states that this css property doesn't work anywhere. It goes on with code examples of what works(this is what I gave you).
For firefox compatibility, it links to an svg document's blur definition.
To do this specific thing, create an svg file(you can embed this into your document or host it somewhere else):
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<filter id="svg_blur" x="0" y="0">
<feGaussianBlur in="SourceGraphic" stdDeviation="15" />
</filter>
</defs>
</svg>
Add the corresponding url() to your css file like this:
#blur-overlay {
filter: blur(10px);
-webkit-filter: blur(10px);
/* If you're using an SVG in the document */
filter: url('#svg_blur');
/* If you're using an SVG hosted on your web server, say at /static/blur.svg */
filter: url('/static/blur.svg#svg_blur');
}

how do i recreate this google plus effect on a place image

I was just looking at a google plus place and i saw this cool blur effect on an image - the left side of the cover photo, is there anyway to recreate this, i have tried some css blur stuff and a couple plugins although they did not seem to come close to recreating this.
how do you think this is done?
https://plus.google.com/u/0/100447465665053723801/posts
used:
blur.js
foggy.js
.blur {
-webkit-filter: blur(10px);
-moz-filter: blur(10px);
-o-filter: blur(10px);
-ms-filter: blur(10px);
filter: blur(10px);
}
Google use two copies of the image. One regular and one blurred. They then overlay and clip them.
They've generated a blurred image version of the header image. Looking at the HTML/CSS you can find the image here:
https://lh5.googleusercontent.com/-nvDu86PoiAc/Uzylet0y0CI/AAAAAAAAABE/3mMd0DUauUY/s630-fcrop64=1,00411693ffffee64:Soften=1,60,0/Resort%2BPool%2B-%2Bdusk%2B2.jpg
You can do what you're trying to accomplish using CSS blur however.
<div style="position: relative;">
<div style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; filter: blur(50px);">
<!-- your image here -->
</div>
<div style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;">
<!-- your content here -->
</div>
</div>
Obviously, you would not put your CSS inline like that. The image div must be separate otherwise your content will become blurred as well. Also, I should note that there is most likely no way to blur part of an image if that's what you're attempting. You'll need to use the same image twice.

Can someone tell me why my links aren't clickable when I have a div under it?

I am making a widget for my site that displays the author and their social links. It worked fine until I added the blurry background div in the mix. Something about it is not letting me click the links any more. I've tried adding z-index values, but that doesn't seem to help. Check out my fiddle to see my problem. Below is the class in question...
.social_artist_image_wrapper {
position:absolute;
height:126px;
width:580px;
background-size:cover;
opacity:0.3;
-webkit-filter: blur(3px);
-moz-filter: blur(3px);
-o-filter: blur(3px);
-ms-filter: blur(3px);
filter: blur(3px);
}
Because element with position:absolute (or anything other than static are placed on top of position:static elements, unless they have a negative z-index.
Try putting position:relative on your inner area, plus a semi-transparent background colour: demo
Change your CSS to have this:
.social_artist_text_wrapper a {
color:#bbb;
text-shadow:0px 0px 5px #000;
position: relative;
z-index: 2;
}
As others have said, it is because you absolutely positioned the background image. So in order to get the other elements to render in front of it you need to set the z-index property, which only works if the position property is something other than static.
Updated fiddle.
I think it is because your div is not under it, it is on TOP of it. This is because you are using position: absolute. You probably can achieve what you are looking for with position:relative.
Add this style to the ".social_artist_image_wrapper" class
pointer-events:none;
Lacks IE support so the positioning answers above may be better suited to your needs (unless you don't need to support IE8 or IE9)

Categories

Resources