views:

464

answers:

4

IE 6 only support :Hover on <a> then can we make css drop down using :hover on <a>

http://htmldog.com/articles/suckerfish/dropdowns/

This example use JavaScript to add hover on LI

'sfhover' class to li elements in the 'nav' id'd ul element when they are 'moused over' and removes it, using a regular expression, when 'moused out'.

So now we've got the Suckerfish pumping out new classes, the next step is to simply duplicate the :hover selector with 'sfhover' class selectors:

A: 

Try ie7-js.

If javascript is absolutely not an option, you can use one of the many examples of CSS only dropdown menus, such as The ULTIMATE CSS only drop-down menu by Stu Nicholls.

In my opinion, making IE6 behave all around via js is preferable to css hacks that have to be implemented for every feature it lacks.

jball
I don't want to use javascript
metal-gear-solid
Why are you avoiding javascript?
jball
if i can make menu without javascript then i would go for it
metal-gear-solid
Have you examined any of the CSS options out there, such as the one I linked to above?
jball
A: 

Yes that's possible

Jamie Dixon
HOW? without using javascript
metal-gear-solid
A: 

Yes you can as long as you nest the sub menu within the anchor.

<a class="hoverMenu">
  MenuText
  <div class="subMenu">
    SubMenuText
  </div>
</a>

.hoverMenu .subMenu { display: none; }
.hoverMenu:Hover .subMenu { display: block; }

I haven't tested this out for IE6, but this is the basic idea behind an all css menu in any other browser. Not sure if you can nest anchors or not.

NickLarsen
not valid, you can't put a `div` inside an `a` tag.
Joseph Silvashy
no it doesn't work in ie6 http://jsbin.com/uyima
metal-gear-solid
ohh well, funny enough though your link is working great in chrome.
NickLarsen
+1  A: 

You can sort of make it work. The code below displays a functional menu that relies on a:hover to trigger display. However, it comes with a few caveats:

  • Since you can't nest <a /> tags in HTML or XHTML, you're limited to a single level of menu items.
  • For the same reason, you have to use JavaScript onclick event handlers to handle user clicks on menu items.
  • IE6 seems to require a :hover rule on the anchor tag itself in order to trigger hover behavior. Without the #menu:hover rule, the #menu:hover span was ignored. The rule needs to have at least one style assignment in it, and not all properties seem to work (e.g. background-color or border worked, but color didn't).

Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>CSS Menu in IE6</title>
    <style type="text/css">
        #menu {
            background-color: #cccccc;
            color: black;
            text-decoration: none;
            position: relative;
        }

        #menu span {
            display: none;
        }

        /* I'm using <b /> tags for the submenu items, just to make the styling easier. */
        #menu span b {
            display: block;
            font-weight: normal;
        }

        /* IE6 seems to require some :hover rule on the anchor element itself.
           Without it, the '#menu:hover span' rule below is ignored. */
        #menu:hover {
            border: none;
        }

        #menu:hover span {
            background-color: #cccccc;
            cursor: pointer;
            display: block;
            position: absolute;
            top: 1em;
            left: 10px;
        }
    </style>
</head>
<body>
    <div>
        <a href="#" id="menu">Menu
            <span>
                <b onclick="alert('Item 1!');">Item 1</b>
                <b onclick="alert('Item 2!');">Item 2</b>
                <b onclick="alert('Item 3!');">Item 3</b>
            </span>
        </a>
        <p>
            Lorem ipsum doler sit amet...
        </p>
    </div>
</body>
</html>

UPDATE: IE6 does sort of work with nested <a /> elements. I tried embedding a link within a submenu, and it displayed properly but mousing over the inner link caused the outer link to lose :hover, and the menu would disappear out from under the cursor.

However, apparently if you wrap the menu in a table (as demonstrated here), it will work. Note that the below code works, but won't validate and might blow up in other browsers:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>CSS Menu in IE6</title>
    <style type="text/css">
        #menu {
            background-color: #cccccc;
            color: black;
            text-decoration: none;
        }

        #menu ul {
            display: none;
        }

        #menu:hover {
            border: none;
        }

        #menu:hover ul {
            background-color: #cccccc;
            display: block;
            margin: 0;
            margin-left: 10px;
            padding: 0;
            list-style-type: none;
        }
    </style>
</head>
<body>
    <div>
        <a href="#" id="menu">Menu
            <table border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td>
                        <ul>
                            <li><a href="http://www.google.com"&gt;Google&lt;/a&gt;&lt;/li&gt;
                            <li><a href="http://www.yahoo.com"&gt;Yahoo&lt;/a&gt;&lt;/li&gt;
                            <li><a href="http://www.stackoverflow.com"&gt;Stack Overflow</a></li>
                        </ul>
                    </td>
                </tr>
            </table>
        </a>
        <p>
            Lorem ipsum doler sit amet...
        </p>
    </div>
</body>
</html>
Annabelle
then how this menu is working fine in IE6 without js http://www.grc.com/menu2/invitro.htm
metal-gear-solid
@Jitendra, why are you still asking the question while you are linking to solutions that fit your constriants?
jball
Ah, clever. I saw that IE would display a nested link properly, but it wasn't playing nice with `:hover`. Apparently wrapping the menu in a table fixes that. Horribly non-standard, but if you don't care about validation then I guess it will work. My answer has been updated with an explanation and example.
Annabelle
It's also worth noting the tricky conditional comment usage on that page. They've structured them such that IE6 will see the table-wrapped `<ul />` inside the top-level menu anchor element, but all other browsers will see an unwrapped `<ul />` *next to* the anchor element.
Annabelle