views:

69

answers:

2

I have a menu that will be automatically created in an asp.net page. I'm trying to use a pure CSS cross browser menu but how can i set it so that each subsequent child is autohiden/shown w/o having to define the style for each level of the menu.

Is this the only way to accomplish this with css?

Essentially im looking for a way to use css to show/hide the child menu items w/o having to define the style for every level - especially since i dont know how many levels there will be.

A: 

When you want to affect each child individually, but without having to make style rules for each of those children, then you need more logic, which CSS doesn't provide. You could use something like PHP for that logic, or you could go with Javascript/jQuery. In that case, you can toggle CSS classes on child[x] through jQuery, and you only need to style those classes. Then it wouldn't matter which child got the class, it would be styled accordingly. Note that you should first make sure your menu is at least usable without Javascript, so users aren't dependent upon it.

stephenhay
+1  A: 

you should be able to do it by only specifying down to the second level

<html>
<head>
<style>
.mnusub li ul{ display:none; }
.mnusub li:hover > ul{ display: block; }
</style>
</head>
<body>
<ul class="mnusub">
    <li>test1
     <ul class="mnusub">
      <li>test2</li>
      <li>test11
       <ul class="mnusub">
        <li>test3</li>
        <li>test4</li>
        <li>test5</li>
       </ul>
      </li>
     </ul>
    </li>
    <li>test5
     <ul class="mnusub">
      <li>test6</li>
      <li>test7</li>
      <li>test8</li>
     </ul>
    </li>
    <li>test9</li>
    <li>test10</li>
</ul>
</body>
</html>

The key here is the ">" selector as it specifies direct descendants and not sub-descendants

enjoy

Mike Valstar
See: http://www.w3.org/TR/CSS21/selector.html#child-selectors
Mike Valstar
ooohhh >, i will try that and update . sounds very promising
schmoopy
Can't seem to get this working IE 7. Any ideas why?
Andy Rose
exactly what i was looking for, thank you.
schmoopy
Andy -- i tested in IE7 and it works for me?
schmoopy
@Andy-rose you might need to squish the names so that there are no spaces between the chevron and the selected elements in some versions of IE.. i always forget which.
Mike Valstar