Question

I need to create a DHTML menu with the specified features, but I can't figure out how to do it. Here's what I need:

All items are layed out horizontally. If they would be wider than the screen, two little arrows appear on the right side of the menu that allow to scroll it. Something like this:

+--------+--------+-------+---+---+
| Item 1 | Item 2 | Item 3| < | > |
+--------+--------+-------+---+---+

Menu items should be clickable anywhere in the cell. They should stretch both vertically and horizontally to the contents. The text in the items should be centered both vertically and horizontally. The menu should work in IE7/Opera/FF/Safari.

The scrolling is the easy part - I just place it all in a container (say, a <div>), set the container to overflow: hidden and then play around in Javascript with clientWidth, scrollWidth and scrollLeft. That I've figured out and have already tried.

But how to make the menu items so stretchy, clickable anywhere and centered text?

Was it helpful?

Solution 2

OK, I talked with my superiors and they decided that it might be OK that you cannot right-click a menu item and select "Open in New Window". If this requirement is dropped, then I'm not bound to <a> elements for links. With JavaScript I can turn anything into a link. Thus, I choose you, pikachoo <table>!

Yap, it's a heresy, but it works. More specifically, it's the only construct that I can think of that can do all of the following at the same time:

  • Center text horizontally and vertically;
  • Stretch to contents horizontally and vertically;
  • Not wrap to next line when items are starting to overflow.

Anything else that can do the same will probably be more convulted anyway. And before anyone has the idea - no, I don't need search engine support. It's an internal web application. It'd be pretty bad if Google could index that...

OTHER TIPS

Try the CSS below:

#menu {
  display: table; 
}   
#menu a {
    display:table-cell; 
    vertical-align:middle;
}

And then format your menu like:

<div id="menu">
    <a href="#">normal text</a>
    <a href="#"><big>large text</big></a>
    <a href="#"><span style="line-height:100px;">very tall text</span></a>
</div>

This will force vertical alignment and prevent the links from wrapping. Let us know how it works out.

clickable anywhere is easy: you can either bind the onclick event trigger (and hopefully some cursor styling) to the atomic cell element, or you can make the atomic cell elements <a> tags (or more likely wrap these in <li>) and link and style appropriately (padding, margin, foo).

e.g. case 1:

<ul id="menu"><li class="item" onclick="foo()" style="cursor:pointer; cursor:hand; padding:1em; margin:1px; float: left;">FOO!</li></ul>

(obviously I don't really recommend inline styling or script handlers but you get the idea)

Applying padding will effectively centre the text, and having no width assigned they'll naturally stretch to fit their content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top