views:

1396

answers:

5

Hi folks,

I have a CSS hover menu which works in all browsers except... surprise -- IE6!

#menu_right ul li:hover ul { visibility: visible; }

This ul is hidden initially, obviously. When I hover over its parent li, it should show up... but it doesn't.

To try to pinpoint the problem, I've tried making the ul initially visible and had the hover action take on something else. For example:

#menu_right ul li ul { visibility: visible; }

#menu_right ul li:hover ul { background: red; }

This doesn't help. On other browsers (including IE7+), the ul will turn red when I hover over its parent list element. But not in IE6. What am I missing?

+2  A: 

IE6 doesn't know the CSS :hover pseudo-attribute, when it appears on anything than a link element. You will have to use JavaScript for this. Try conditional statements, and if you use jQuery, you can code the hover effect for IE6 in 3 (+/- formatting) lines:

<!--[if lt IE 7]>
<script type="text/javascript">
$('#menu_right ul li').hover (function () {
  $(this).addClass ("hover");
}, function () {
  $(this).removeClass ("hover");
});
</script>
<style type="text/css">
#menu_right ul li.hover {...}
...
</style>
<![endif]-->

Mark, that in the CSS statements I used the dot instead of the colon.

Cheers,

Boldewyn
Thanks for the quick response. But there infact IS an implementation of anything:hover on IE6, using purely CSS. I'm trying to avoid using all those javascript and broswer-specific hacks.http://www.cssplay.co.uk/menus/final_drop.htmlThis guy's code plops everything inside a table if he encounters IE6. I'm not really sure why this works, but I've tested his demo on IE6 and it does. I've tried to mimic this but to no avail.
Tal
You could also use an `<a>` instead of an `<li>`; that no longer requires Javascript, though you'll have to jump through quite a few other loops trying to get the `<a>` tag to render similar the `<li>` (broken `display: inline-block` support... gaah HATE IE).
ephemient
Besides, IIRC IE4 didn't recalculate CSS properties when dynamically changing classes...
ephemient
Does anybody support IE4 anymore?
voyager
Hi. If you look at the source of Stu's example, you find conditional comments spread around the HTML source. While his solution is really nice (and CSSPlay is kind of a reference for many CSS tricks), I feel quite uncomfortable with messing up my markup with stuff only for one stupid browser. That also applies to Eli's "insert a spare `<a>`" (while it actually *does* what it should). My solution is: Plain standards for everyone, single place fixing for the bad guy.
Boldewyn
By the way: If you _don't use_ jQuery already, it's overkill to add it for just this task. It's quite easy to implement the above in plain JS, I just thought, the jQuery-fied code is more self-explanatory.
Boldewyn
@ephemient: IE8 supports `inline-block`, but it took me really long to figure out you have to put it in IE8-mode (that is, X-UA-compatible). Why on earth do the MS guys still develop their own browser? Why don't they just say: "Well, we give it away for free anyway, so just build a new one on top of WebKit!"? Or simpler yet: Microsoft: Buy Opera!
Boldewyn
+2  A: 

No :hover on anything but <a>... God I love this browser.

Try to use :hover on a conveniently-located <a> (if it's a list of links, like most CSS hover menus, it won't be a problem ), or just go with Javascript, as already suggested.

Eli Krupitsky
+3  A: 

You should use something like this

<ul>
  <li><a href="#"></a></li>
  <li><a href="#"></a></li>
  <li><a href="#"></a></li>
</ul>

and style the <a> instead of the <li>. You just have to make sure that you size the a to be the exact same size as its enclosing li.

div.menu ul.menu {
    margin:0;
    padding:0;
}

div.menu ul li {
    margin:0;
    padding:0;
}

div.menu ul.menu a {
    display:block;
    height:22px;
    margin:0;
    overflow:hidden;
    padding:0;
    width:252px;
}

The reason you are seeing that it works on every browser except IE6, is that it supports :hover only on <a> elements.

voyager
If it weren't for IE, I'd recommend getting rid of useless `href="#"` (`<a>` is legal without), but I seem to recall some IE versions only applying `:hover` on `a[href]` elements...
ephemient
+1  A: 

Take a look at whatever:hover http://www.xs4all.nl/~peterned/csshover.html. This baby solves all sorts of weird IE6 hover problems, might solve yours.

PHLAK
A: 

this link really worked for me to solve IE6 li:hover problem. http://www.kavoir.com/2009/01/css-selectorhover-hack-for-ie6.html . itz an Easy Fix :-)

devang