Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make :active state work in IE?

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;
}
like image 612
rahim asgari Avatar asked Oct 30 '10 08:10

rahim asgari


1 Answers

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.

like image 180
Ben Lee Avatar answered Oct 10 '22 13:10

Ben Lee