CSS3 - Creating a Basic "blur" effect.

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
Just something small to post. It was something I was playing around with the other day, intentionally for a NSFW filter (don't ask) however I never got around to finishing it.

Nevertheless, I thought I'd share how to create a basic CSS blur effect. This example will be on hover.. For the full demo and code, .

Creating a blur using filter
It's a lot easier than it seems. We shall be creating a blurred image by applying the following CSS to our image attribute (or class).
Code:
img {
  -webkit-filter: blur(20px);
  -moz-filter: blur(20px);
  filter:blur(20px);
  transition: 1s ease-out;
}
For this example I have just used the img tag in HTML and applied the pure basic CSS. Notice how, in brackets, is the blur amount. I have applied 20px as it almost makes the images impossible to work out. Notice have I have also applied a transition? That will be for later when it comes to animation on hover.

For my HTML I have created a class (which will be used later) and the child element inside of that is my img tag. As seen below:
HTML:
<div class="image" id="nsfw">
  <img src="http://lorempixel.com/output/sports-q-c-640-480-5.jpg" alt="some image">
</div>

Now for some animation.. This is just to make it a little fancy upon hover (all using pure CSS). For that I have used the code below:
Code:
.image:hover img,
.image:hover ~ img {
    -webkit-filter: blur(0);
}

For this we use the :hover effect for both the img and we also use the general sibling selector (for some reason it fails work without it) but this basically selects any child element of the class .image, directly the img tag.

That's not all... There are also plenty of other filters that can be used. A list can be seen below:
  • blur()
  • brightness()
  • contrast()
  • url()
  • drop-shadow()
  • grayscale()
  • hue-rotate()
  • invert()
  • opacity()
  • sepia()
Eh, this was just a post because I think some pretty cool things can be done with these effects - they aren't that new at all.. Just haven't seen it posted around here before so I thought I'd share. Let us know how you get on below!
 

Users who are viewing this thread

Top