While looking for keyboard-accessible menus, I stumbled across this question, which has as its answer a CSS drop-down menu http://jsfiddle.net/cfWpE/. It seems to use styling on the anchors in the menu rather than :hover on the <ul> items to display the submenus without any Javascript, but I can’t figure out how.
Could someone who’s better than me at CSS explain how this works? I’d like to try to extend this to a 3-level menu, but without understanding how it works for two levels, that’s going to be difficult.
edited for clarity:
It’s not actually the keyboard part that confuses me; I understand that tabbing through updates :focus on the current focus link, but the only CSS rule that seems to be applied to those elements is
ul.menu li.list a.category:hover,
ul.menu li.list a.category:focus,
ul.menu li.list a.category:active {
margin-right:1px;
background: black;
}
I don’t understand how setting margin-right to 1 pixel makes the parent <li> visible.
This is an interesting Technique to achieve a dropdown menu.
The list items
.listhave a very high negativemargin-topand a width of250px. This places their content out of the viewport of the browser. The child anchorsa.categoryhave a positive margin-top with exact the same value, so they are visible to the user as if they were positioned normally. Now both, thea.categoryand theul.submenuhave afloat:leftapplied. that’s why the submenu does not appear beneath the anchor, but beside it. (But it has no margin-top, so it is still “invisible”) Both elements (a.categoryandul.submenu) have125pxwidth and fit perfectly into the parentliwhich has a width of250px. Now on hover the anchor gets an additional1pxmargin. This makes both elements too wide to fit into the parent container side to side and so the floated submenu breaks onto a newline and suddenly appears below the anchor – TADA:-DI hope you could follow my explanation – if not ask please which part I need to clarify;)
Extending this to a third level is not possible – I would just go with a regular css-menu with
display:block;andhide. However, you can use absolute positioning and switch thetopvalue from a very high negative value to0when hovering, which would have the same effect.Generally, i would use this with care. Some searchengines consider text that is hidden via negative margins or text-indent as blackhat SEO and may penalize one for that. Although it might be possible that Google is clever enough to recognize this as a regular dropdown-menu