I'd like to make my background image blur for let's say 5px when you hover the link with a mouse cursor. Is there any simple way to make this happen? I got a bit entangled with classes and id's here...
#pic {
background: url(http://www.metalinjection.net/wp-content/uploads/2014/07/space-metal.jpg);
background-attachment: fixed;
background-repeat: no-repeat;
background-size: cover;
height: 500px;
/*blur using this function*/
filter: blur(0);
-webkit-filter: blur(0);
-moz-filter: blur(0);
-o-filter: blur(0);
-ms-filter: blur(0);
}
.banner_link {
font-family: 'Raleway';
letter-spacing: 0.2em;
font-size: 13px;
color: #ffffff;
text-align: center;
line-height: 16px;
padding-top: 45px;
text-transform: uppercase;
}
.banner_link a:after {
content: '';
display: block;
margin: auto;
height: 1px;
width: 90px;
background: #ffffff;
transition: width .2s ease, background-color .5s ease;
}
.banner_link a:hover:after {
width: 0px;
background: transparent;
}
.banner_link a:hover #pic {
filter: blur(5px);
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
}
<div id="pic" class="banner">
<div class="banner_link"><a>Link</a>
</div>
</div>
This is the easiest way to create the effect you are looking for. As I'm sure you know, the issue is one of scope. You cannot (to my knowledge) set the background of a parent element from a child element without a JS workaround.
This issue is that you are attempting to traverse up the document tree with CSS. There is no parent selector in CSS, therefore you can only rely on JS to toggle the blur effect when the inner
<a>
element is hovered on.This can be easily achieved using native JS, but I've chosen to use jQuery because of the relative ease of use.
The trick is quite simple: to absolutely position a blurred version of the background image, nested in a pseudo-element, say
::before
, with its opacity set to zero. When the cursor is over the inner<a>
element, toggle a class, say.blur
, which sets the pseudo-element's opacity to 1.The reason why we can't use JS to set the CSS properties of the pseudo-element is because it is not accessible to JS.
Implementation in jQuery
This is the working one. When you hover the link it will make the image blur via jQuery.
See the jsfiddle link below
Code
Implementation in CSS
See this working jsfiddle if you want to achieve it via CSS.
Code