BB Design BB Design - 1 month ago 5x
CSS Question

Aligning drop-down nav to main menu with CSS

I have a CSS-only drop-down nav technique that I use frequently. Here is the simplified HTML:

<ul id="nav">
<li><a href="#">Menu Item</a>
<span class="subnav">
<a href="#">Submenu Item</a>
<a href="#">Submenu Item</a>
<a href="#">Submenu Item</a>

Here is the simplified CSS:

#nav1 li:hover span.subnav{display:block}
#nav1 li{position:relative;}

The problem is in positioning sub.subnav vertically, where I have top:20px. I need that element to be positioned exactly at the bottom of the LI element. If it is too high, it overlaps the main menu item. If it is too low, there is a gap between the main menu item and the sub-nav, and when the mouse passes over that gap, the sub-nav closes unexpectedly and the user is frustrated.

I can usually get it exactly right, down to the pixel (in this example, 20px). But some browsers seem to not render fonts exactly the same. The height of the LI element is determined by the font inside it, so there can be some difference and then my 20px calculation is not correct.

Is there any other way in CSS to position my span.subnav element to the bottom of the LI element?

Thank you.


I think it is okay to have the top CSS property of .subnav to be auto

#nav li { position: relative; }
#nav li:hover span.subnav { display: block; }
#nav li .subnav { top: auto; }