How to make jQuery UI nav menu horizontal?

I admire all these efforts to convert a menu to a menubar because I detest trying to hack CSS. It just feels like I’m meddling with powers I can’t possibly ever understand! I think it’s much easier to add the menubar files available at the menubar branch of jquery ui.

I downloaded the full jquery ui css bundled file from the jquery ui download site

In the head of my document I put the jquery ui css file that contains everything (I’m on version 1.9.x at the moment) followed by the specific CSS file for the menubar widget downloaded from the menubar branch of jquery ui

<link type="text/css" href="https://stackoverflow.com/questions/12944364/css/jquery-ui.css" rel="stylesheet" />
<link type="text/css" href="css/jquery.ui.menubar.css" rel="stylesheet" />

Don’t forget the images folder with all the little icons used by jQuery UI needs to be in the same folder as the jquery-ui.css file.

Then at the end the body I have:

<script type="text/javascript" src="https://stackoverflow.com/questions/12944364/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.9.0.custom.min.js"></script>
<script type="text/javascript" src="js/menubar/jquery.ui.menubar.js"></script>

That’s a copy of an up-to-date version of jQuery, followed by a copy of the jQuery UI file, then the menubar module downloaded from the menubar branch of jquery ui

The menubar CSS file is refreshingly short:

.ui-menubar { list-style: none; margin: 0; padding-left: 0; }
.ui-menubar-item { float: left; }
.ui-menubar .ui-button { float: left; font-weight: normal; border-top-width: 0 !important; border-bottom-width: 0 !important; margin: 0; outline: none; }
.ui-menubar .ui-menubar-link { border-right: 1px dashed transparent; border-left: 1px dashed transparent; }
.ui-menubar .ui-menu { width: 200px; position: absolute; z-index: 9999; font-weight: normal; }

but the menubar JavaScript file is 328 lines – too long to quote here. With it, you can simply call menubar() like this example:

$("#menu").menubar({
    autoExpand: true,
    menuIcon: true,
    buttons: true,
    select: select
});

As I said, I admire all the attempts to hack the menu object to turn it into a horizontal bar, but I found all of them lacked some standard feature of a horizontal menu bar. I’m not sure why this widget is not bundled with jQuery UI yet, but presumably there are still some bugs to iron out. For instance, I tried it in IE 7 Quirks Mode and the positioning was strange, but it looks great in Firefox, Safari and IE 8+.

Leave a Comment