6

Using IE with the HTML below, I can't get the input's background to change to red if I hover my mouse immediately over the word "Test". It does, however, change to red if I hover over the area to the right of the word "Test" but still within the bounds of the input element. This doesn't happen in Firefox or Chrome. What is the proper way to define my styles to get this to work correctly in IE?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title></title>
    <style type="text/css">
        input { border: 0px; margin:0; padding:0; }
        input:hover { background: red }
    </style>
  </head>
  <body>
    <input type="text" value="Test" />
  </body>
</html>
7
  • Have you tried setting the input to display:block? I've noticed that kind of behavior with inline elements. Commented Mar 8, 2011 at 15:09
  • Setting display to block doesn't help. Commented Mar 8, 2011 at 15:12
  • None of the four answers so far has got the right end of the stick. The described issue happens in IE8 and IE9 (I didn't test older). If you hover directly over the text in the input element, the hover doesn't work. Commented Mar 8, 2011 at 15:24
  • what if you use a class, let's say theInput and then use theInput:hover? Does it produce the same result? Commented Mar 8, 2011 at 15:25
  • This seems to be a bug in IE9. It works for me when I hover over the empty part of the input element, or when I remove the input { border: 0px; margin:0; padding:0; } declaration completely. I have not been able to test on IE8. Commented Mar 8, 2011 at 16:17

7 Answers 7

2

This appears to be a bug in how IE handles the hover event. In this example I changed the hover to form:hover input which should trigger no matter where you are. But it is still buggy.

http://jsfiddle.net/Xb8Bg/74/

In my testing I found that the border triggers the hover which then allows it to 'work.' However, you can still move your mouse very quickly into the center of the text and reproduce the bug. So that tells me the overall implementation is bugged.

The best workaround I could come up with is to use a transparent border. This of course suffers from the moving mouse fast bug I just mentioned, but it is better than nothing. I suspect a js solution could be easily devised if you really need the 0px border.

input:hover { border: 1px solid transparent; }
Sign up to request clarification or add additional context in comments.

Comments

2

This is actually caused by the fact that IE does not recognize the :hover psuedo class on non-anchor (<a>) elements unless you use STRICT document type:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

http://jsfiddle.net/durilai/Xb8Bg/1/

3 Comments

Even IE8 doesn't evaluate hover over input's text.
I'm actually using IE 9 beta and adding the STRICT doc type doesn't help.
@Jaroslav Jandek - Correct, I updated my answer. It worked in jsfiddle, but only first time, so I think it is just a bug there.
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru">

This will enable :hover support and other stuff for IE

Comments

1

You can use javascript to enable that.

//css
input{
   border: 0px; 
   margin:0; 
   padding:0;
}

input.hover{
  background:red;
}

//javascript
$(document).ready(function() {
  var input = $("input");
  input.mouseover(function(e) {        
    $(this).addClass("hover");
  }).mouseout(function(e) {
    $(this).removeClass("hover");
  });
}); 

Comments

0

Provide height and width of Input box

input { border: 0px; margin:0; padding:0;width:200px;height:25px; }

1 Comment

This only works if you move the mouse very slowly. If you quickly move the mouse to be over the text, it doesn't trigger the hover.
0

This is a problem with your DOCTYPE as Dustin Laine pointed out.

Experiment with the HTML 5 doctype or some others.

http://jsfiddle.net/q7Ymj/

Comments

-1

IE only supports :hover on links. You'll have to change the style with js.

1 Comment

Awesome. I have to commit to memory that IE != IE 6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.