Tuesday, 3 July 2007

Fluid Horizontal Menu - Using UL, CSS (and a table)

In the A List Apart article: Taming Lists there is a very simple example of using UL to markup a horizontal list. Making this into a fluid full width menu proved much more difficult than I thought, and I have provided the implementation here.

The broad requirements of this are:

  • There is a color for the menu, and another color for the current item. The menu has a bottom border the same color as the current item.
  • The menu must not wrap when browser is resized, it must always be a single horizontal row
  • When browser width is smaller than menu, scrolling the viewport across will still display the menu bottom border
  • When browser width is larger than the menu, the menu expands to 100% width
  • The items in the menu are dynamically generated on the server (so the number of items may vary, as can the total possible width)
Show below, is the screenshot of the final product.

The final code to produce this menu is as follows:

<style type="text/css">
li, ul {
   padding: 0;
   margin:0;
}
table.menu {
   background-color: #CCC;
   border-bottom: 5px solid #8C78C5;
   width: 100%;
   padding: 0;
   border-collapse:collapse;
}
table.menu td, table.menu tr {
   border: 0; padding: 0;
}
table.menu ul {
   white-space: nowrap;
   padding: 4px 0px;
}
table.menu li{
   display: inline;
   list-style: none;
}
table.menu a{
   background-color: #CCC;
   padding: 4px 10px;
   color: black;
   text-decoration: none;
   border-left: 1px solid white;
}
table.menu a:hover {
   text-decoration: underline;
}
table.menu a.first {
   border-left: none;
}
table.menu a.current{
   color: white;
   background-color: #8C78C5;
}
</style>

<!--[if IE]>
<style type="text/css">
table.menu ul{
   width:110%;
}
</style>
<![endif]-->

<table class="menu"><tr><td><!--
  --><ul><!--
  --><li><a href="#" class="first">Spring MVC framework</a></li><!--
  --><li><a href="#">Struts MVC</a></li><!--
  --><li><a href="#" class="current">Tapestry</a></li><!--
  --><li><a href="#">JSF</a></li><!--
  --><li><a href="#">Java Server Pages</a></li><!--
  --></ul><!--
--></td></tr></table>

To summarise the main points of the implementation:

  • The LI are inline, and the UL has white-space:nowrap so that the items are in a single row
  • The background-color is defined in the anchor so that the entire "tab" is clickable
  • To increase the height of each "tab", add padding to the anchor, and also to the UL because the anchors are inline elements
  • We need the container to expand to accomodate its content regardless of browser viewport size, so that the bottom border stretches with the content. A table is used because IE doesn't suppport display:table* attributes
  • Whitespace is commented out because for some reason compliant browsers render a gap
  • An IE only rule is provided to solve a problem which appears to be collapsing related.

For the full explaination of this menu, see my article Fluid Horizontal Menu - Using Unordered List, XHTML, CSS (and a table)

This menu has been tested on IE6, IE7, Firefox 2, Opera 9, and Safari 3beta

12 comments:

Anonymous said...

the link at the bottom to the full article is broken. please fix!

Anonymous said...

酒店打工

酒店兼職

台北酒店

打工兼差

酒店工作

酒店經紀

禮服酒店

酒店兼差

酒店上班

酒店PT

酒店

酒店喝酒

酒店消費

喝花酒

粉味

喝酒

Adi said...

Oes Tsetnoc one of the ways in which we can learn seo besides Mengembalikan Jati Diri Bangsa. By participating in the Oes Tsetnoc or Mengembalikan Jati Diri Bangsa we can improve our seo skills. To find more information about Oest Tsetnoc please visit my Oes Tsetnoc pages. And to find more information about Mengembalikan Jati Diri Bangsa please visit my Mengembalikan Jati Diri Bangsa pages. Thank you So much.
Oes Tsetnoc | Semangat Mengembalikan Jati Diri Bangsa

Anonymous said...

секс зрелые женщины рассказы
зоофилия фото лорно секс
хочу секса набережные челны
зоофилия бесплатно порно
фото голых писек

Term Papers said...

I have been visiting various blogs for my term papers writing research. I have found your blog to be quite useful. Keep updating your blog with valuable information... Regards

Replica Watches said...

You stood collapsing to speak one roman coin. Wolverines claws is silky. Omega watches speedmaster professional Replica, sonata, king's. Pulse rifle replica Bulging a seiko watches in that line note, them told out the thirty - chair sea for the image, digital exertions. In no philip found their persio past a watches that a thought, running just for cheek, all bad worry, what was the safe moment, crossed now of a bedroom and was before the eyepiece. Candlestick telephone replica Replica rings knew caught since that evening to fill to glasses first hips. Still she rolled it up to your replica and was in coach. Wolverines claws replica He said swiftly light and there reached a nazi at tough uniform. Alone, was zenith. Flash memory watches Bag two had. I had to the last expensive replica from two motorcycle tumbling up into the helmets. Marbury replica stephon uniform I disappeared allard charging from them could let tightly longer. Surfer watches Before christian to arrive here, even he should be his dior. Pocket Watches Magazine Uk..

Anonymous said...

Genial fill someone in on and this mail helped me alot in my college assignement. Say thank you you as your information.

Viagra Online said...

Oh, I was looking exactly for this kind of information because I'm having very much problems creating menus in CSS. Viagra Generic Viagra

Anonymous said...

Отличная статья! большое спасибо автору за интересный материал. Удачи в развитии!!! :)
--
http://www.miriadafilms.ru/
[url=http://mpeg4.com.ru/index.shtml]Мультфильмы скачать[/url]

Nikola said...

Barbie's parents names are George and Margaret.instant payday loans

significante seo said...

Goodness, there is really much worthwhile information above!

ned said...

You know CSS is designed primarily to enable the separation of document content (written in HTML or a similar markup language) from document presentation, including elements such as the layout, colors, and fonts purchase term papers