CSS hover selector fix for IE6

Tags: CSS
Word Count: 864

The hover selector is wonderful css technique that can add style to an element as well as improve usability. Most people use it to style links so that users are aware of the link they are hovering over. One common technique is to underline all of the links on a page and then remove the underline when a user hovers over the link. Again this improves both the look of the page as well as usability. Another technique I enjoy is changing the background color of a row when a user hovers the row. This can be achieved by simply using the :hover pseudo-class. Here is a screen shot of my example.

The problem with this technique is it works in all browsers except IE6, of course. IE6 only supports the :hover pseudo-class on anchor () tags. So how can we fix this so that all users see our row highlight? There are at least 2 solutions that I know of. The first involves a little JavaScript and I found a great example of how to do this on www.sidesofmarch.com. The example basically uses prototype to grab all of the rows and apply a class to them. You could easily do this in jquery, ext or spry as well.

Event.observe(window, 'load', function() {
   if (!window.XMLHttpRequest) {
      // IE6, older browsers       //new HoverBehavior('tr');
      $$('table.hover tr').each( function(e) {
         Event.observe(e, 'mouseover', function() {
         Element.addClassName(e, 'hover');
         });
         Event.observe(e, 'mouseout', function() {
         Element.removeClassName(e, 'hover');
         });
      });
   }
});

The second solution is to use the csshover behavior that was developed by Peter Nederlof. IE supports so called behaviors; .htc or .hta files that can be attached to specific elements using css making it possible to let these elements behave in a special way. You can visit Peter's site to learn more about it. Once you download the csshover.htc file you simply add the following to your stylesheet.

body { behavior:url("csshover.htc"); }
This will look parse your stylesheet and search for all :hover rules and replace the rule with a scripted event. I really like this solution because it enables you to use standard code for IE7 and FireFox while using this to apply a "fix" to IE6. Here is the final code for my example, enjoy!

index.cfm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""DTD/xhtml1-strict.dtd"> <html>
<head>
   <title>hover test</title>
   <link href="stylesheet.css" type="text/css" rel="stylesheet">
<body>

   <table cellpadding="0" cellspacing="0" border="0">
   <caption>Cleveland Area ColdFusion Programmers</caption>
   <thead>
   <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Email Address</th>
      <th>Website</th>
   </tr>
   </thead>
   <tbody>
   <tr>
      <td>Dan</td>
      <td>Vega</td>
      <td>danvega@gmail.com</td>
      <td>http://www.danvega.org/blog</td>
   </tr>
   <tr>
      <td>Brian</td>
      <td>Meloche</td>
      <td>bmeloche@gmail.com</td>
      <td>http://www.brianmeloche.com</td>
   </tr>
   <tr>
      <td>Todd</td>
      <td>Sharp</td>
      <td>cfsilence@gmail.com</td>
      <td>http://www.cfsilence.com</td>
   </tr>
   </tbody>
   </table>
   
</body>
</html>
stylesheet.css
body {
   behavior:url("csshover.htc");
}
table {
   font-family:Arial,helvetica,sans-serif;
   font-size:12px;
   border:1px solid #ddd;
   border-collapse:collapse;
}
caption,th,td {
   padding:4px;
   border:1px solid #ddd;
}
caption {
   background:#ddd;
   color:#242424;
}
tbody tr:hover {
   background: #242424;
   color:#fff;
   cursor:pointer;
}

Comments

#1 Posted By: Dondre Posted On: 1/22/08 3:32 AM
I am having this problem with the dropdown navbar that I am trying to make compatible with IE6. I don't use tables but instead only divs for the layout. The csshover.htc or csshover2.htc files haven't given me any success. Any other recommendations?
#2 Posted By: Roger Gordon Posted On: 1/28/08 11:24 AM
Thanks so much for this post, and especially the link to Peter Nederlof's technique. I'd totally given up on an elegant solution for IE6's :hover issues, so I was very surprised to find this solution.
#3 Posted By: Andy Posted On: 2/17/08 5:45 PM
thanks for posting this excellent fix for one of IE6's many annoying inconsistencies! specifically the link to peter's site the csshover.htc is a fantastic fix for a background image swap on divs that i've been working with. thanks again!
#4 Posted By: Livingston Samuel Posted On: 2/20/08 8:46 AM
here is the easiest and simple method to make the hover function in IE6 or any other browser .

http://blog.delivi.info/javascript/hover-for-non-a...
#5 Posted By: Donald Howe Posted On: 3/26/08 12:07 PM
I am trying to use the csshover behavior that was developed by Peter Nederlof. But I can't get it to work, or I can't see that it is working. I downloaded the csshover.htc file and placed in my include folder.
This is the site; http://dev-westfield.virtualhorizons.com/careers/w...
The menu rollovers work fine but the people rollovers do not work in IE6. I am using the :hover in my CSS.

Any help appreciated.
#6 Posted By: webfairy Posted On: 9/1/08 7:59 PM
OH MY GOD! Thank you! :-D
#7 Posted By: Larry Posted On: 11/6/08 12:42 PM
Thanks for this reference. It really helped me figure out how this works.

I just needed a hover effect on a single div, so based on everything i learned here i came up with the following code, which works in IE and Firefox:

<div id="foo" onMouseOver="this.className += ' ' + 'hover';" onMouseOut="this.className = this.className.replace('hover', '');">

and in your styles you just need
#foo { whatever }
and
#foo.hover { whatever }

Thanks again, because i never would have figured this out without the info here!
#8 Posted By: Lostboy79 Posted On: 1/13/09 10:21 AM
I couldn't get any of this to work but I stumbled across this, which is by far the simplest solution in my book.

http://www.basictips.com/table-cell-background-col...
#9 Posted By: Pete Posted On: 1/22/09 7:37 PM
Great article and comments!
Larry's solution posted 11/6/08 works great when you have a small number of divs to tweak and doesn't require the csshover3.htc file.
#10 Posted By: Will Posted On: 9/15/09 4:30 PM
flawless and clean implementation, many thanks


Post Your Comment

Leave this field empty







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.