views:

39

answers:

2

I have a button in my html form and need to change it's background image when it is clicked using css. it works perfect in FF but it seems that IE doesnt support :active state.

Here is my code:

HTML:

<button class='button'>Click Me</button>

CSS:

.button {
    width: 118px;
    height: 33px;
    background: url(/images/admin/btn.png) no-repeat center top;
    border: none;
    outline: none;
}
.button:active {
    background-position: center bottom;
}
+4  A: 

This is a known bug in earlier versions of IE (I think they solved it in IE8). I usually solve this (as well as the corresponding "hover" problem) with javascript. I attach two event handlers to the element -- "mousedown" to set an additional class (something like "button-active") and "mouseup" to remove the class. In jQuery it would be something like this:

$('.button').mousedown(function() { $(this).addClass('button-active'); });
$('.button').mouseup(function() { $(this).removeClass('button-active'); });

Then, just add that class to the css rule, like so:

.button:active, .button-active {
    background-position: center bottom;
}

A little ugly, yes, but what do you expect -- it's Internet Explorer. It can't be pretty.

Ben Lee
Actually, this problem usually is for "hover" where the appropriate callbacks are for "mouseover" and "mouseout". I've never tried it for active, so "mousedown" and "mouseup" might not be exactly the correct events to capture -- but you get the idea.
Ben Lee
+1 for the final line.
nico
It seems that i have to use javascript code for the purpose. :(
rahim asgari
I think you're looking for the focusin (http://api.jquery.com/focusin/) and focusout (http://api.jquery.com/focusout/) events.
AgentConundrum