Fluid CSS Menus and Sub-Pixel Workaround |
A long standing CSS problem, best described by John Resig in his blog, is the different approach browsers use when rounding widths calculated from percentages. Firefox rounds alternately up and down in order to make elements fit perfectly, Opera and Safari always round down so that elements will never overflow the container, and Internet Explorer always rounds up so elements often overflow their container and developers get to practise using profanities.
My use case is a fluid css menu with no gaps between the elements or at the end. If you don't know how to build css menus you might want to start by reading my blog on css menus. CSS menus are useful because they are built with <ul> and <li> markup which is a natural representation for hierarchies. The problem is that since menu items are floated, using percentage widths does not guarantee the space will used perfectly (unlike tables).
The basic concept behind the workaround is to leave the last element of the list unfloated so it occupies the entire width of the menu bar underneath the floated elements. Essentially the last item has the following style:
.web_cssMenu li.web_last { float: none; width: auto; }Fluid CSS Menus and Sub-Pixel Workaround
There are, as you might expect, a few issues with this idea:
Most of these problems can all be resolved with a little additional styling. The 3px gap can be fixed by adding a negative margin to the second last element, but this fix makes more code pollution and is not really worth applying. The complete set of changes are then:
.web_cssMenu li { z-index: 2; background: inherit; } .web_cssMenu li.web_last { float: none; width: auto; z-index: 1; }
Of course you need the rest of the css menus styles to get a complete picture of what I'm talking about:
View a complete example of my Fluid CSS Menus and Sub-Pixel Workaround.
There are other problems with floated elements (e.g. heights), but hopefully this is trick will solve a problem for you until something better comes along.
Roger Keays is an artist, an engineer, and a student of life. He has no fixed address and has left footprints on 40-something different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, the proper use of semicolons, and finding good food. |