I have a horizontal menu to which I’m trying to add a drop-down sub-menu. The navbar is supposed to have a top and bottom border and a background but because the li’s are floated it has no height. I can’t use overflow hidden as that will hide the drop-down menu.
I’m rapidly running out of ideas and braincells. I’m sure I could refactor the code with the help of Google but I’m hoping to stay with most of what I have with a few corrections (or am I just dreaming?)
Back in the old days when there were fixed heights on the main list-items that top:unit-value; method would work.
It is better to use top:auto; which places the sub-ul at the bottom of the parent content. That will hide your 1px bottom border on the main ul though so the fix is to add a top border to the sub ul.
Hiding offscreen with negative margins is the way to keep everyone happy.
So if you place all your rules on the sub-ul to begin with, all that is need on hover is the margin rule to show the menu.
This resolves all issue mentioned.
nav ul ul {
position: absolute;
top: auto ; /*doesn't require a known height*/
left: 0;
margin-left: -999em; /*hide with margin for accessibility*/
border-top: 1px solid #999; /*line up with main ul*/
}
nav ul li:hover ul {
margin-left: 0;
}
And then you can test the a href values 1-3 on the sub-list, they now show that keyboard tabbing is working.
I didn’t like the 2.3em as I could see it was a bit hit-and-miss and seemed a bit magic number-ish. Didn’t realise I could use auto there.
Thanks too for the comments on accessibility. I always try to make my sites accessible, and cover that one with a “cheat” by putting a menu of projects on the projects page.