UL Dropdown Menu Problems - css

I have two problems with my dropdown menu, one involving links, the other involving IE7 issues. Code follows after questions, and in both instances, I'm trying to avoid javascript (a key part of the project)
I am successfully highlighting the text of the link when I hover, including some pixels above, below and left & right. However, the only part of the highlight that is clickable (i.e. where I can access a hyperlink) is where the text is, and I want to be able to have the entire highlight, padding and text, to be clickable. I've done it before, but I'm confused with the current code on how to fix it. Can anybody help me out?
Using the same dropdown, everything is working fine, except in IE7. Some IE7 users complained that once they highlighted the menu item and the dropdown occurs, they only get down to about the 2nd item before the dropdown disappears, and it does it for every dropdown. I know it's an issue with IE7, but I need to get a work around for it. Any help at all?
My CSS code:
ul { list-style: none; }
p { margin: 8px 0; }
ul.dropdown { list-style-type:none;height:24px; top:2px; padding:0px 0px 0px 0px;;margin:0px 0px 0 1px;vertical-align:bottom; color:#000000; position: relative; }
ul.dropdown a:hover { color: #000; }
ul.dropdown a:active { color: #ffa500; }
ul.dropdown li { float: left; position:relative; vertical-align:middle; background-position:0 -40px; padding: 2px 4px 5px 2px; margin-right:6px;}
ul.dropdown li a { display: block; padding: 0px 0px; color: #222; text-decoration:none; vertical-align:middle; width:100%;}
ul.dropdown li:last-child a { border-right: none; }
ul.dropdown li.hover,
ul.dropdown li:hover { background: #F3D673; color: black; position: relative; }
ul.dropdown li.hover a { color: black; }
ul.dropdown ul { width: 152px; visibility: hidden; position: absolute; top: 100%; left: -40px; z-index:60;}
ul.dropdown ul li { font-weight: normal; background: #ECEAD8; color: #000; width:100%;/*border-bottom: 1px solid #ccc;*/ float: none; }
ul.dropdown ul li.nohover { color: black; background: #ECEAD8; position:relative; }
ul.dropdown ul li a { border-right: none; width: 100%; display: inline-block; min-height:1.4em;}
ul.dropdown ul ul { left:72.7%;top: 0px; width:158px; z-index:50; display:inline-block;}
ul.dropdown li:hover > ul { visibility: visible; display:block; }
#arrowRight { float:right; margin-top:-11px;}
a.moreItems {background: url(/images/menu/arrow_r.gif) no-repeat right;}
Here is the HTML Code:
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<link rel=stylesheet type="text/css" href="menustylesheet.css">
</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="25" class="topmenu" bgcolor="##ECEAD8" nowrap>
<ul class="dropdown">
<li><b>Item 1</b> <img src="/images/menu/arrow_d.gif" border="0" height="7" width="7">
<ul>
<li><a class="moreItems" href="">Item 1-1</a>
<ul>
<li>Item 1-1-1</li>
</ul>
</li>
<li><a class="moreItems" href="">Item 1-2</a>
<ul>
<li>Item 1-2-1</li>
</ul>
</li>
<li>Item 1-3 <div style="vertical-align:middle;"><img src="/images/menu/arrow_r.gif" id="arrowRight" border="0"></div>
<ul>
<li>Item 1-3-1</li>
<li>Item 1-3-2</li>
</ul>
</li>
<li>Item 1-4</li>
<li>Item 1-5</li>
</ul>
</li>
</ul>
</td>
</tr>
<tr><td bgcolor="##c0c0c0" style="height:1px;"></td></tr>
</table>
</body>
</html>
I apologize if this has been answered before, but I hope someone can pinpoint where in the code I need to change or modify to make it work.

I have an answer to your first problem. When doing this kind of menu. I stay as far away from padding as I can. I find it easier just to use margins, but for starters lets look at a page I built for you:
http://www.albatrossfonts.com/stack/ulbuttons.html
I'll explain this code below.
Here is my HTML:
<div id="wrapper">
<ul class="dropdown">
<li>Button 1</li>
<li>Button 1</li>
<li>Button 1</li>
</ul>
</div>
And the CSS:
.dropdown
{
width: 200px;
display:block;
margin: 200px auto 0 auto;
list-style-type: none;
padding: 0;
}
.dropdown li
{
width: 200px;
height: 44px;
display: block;
float: left;
margin: 1px 0 0 0;
list-style-type: none;
padding: 0;
}
.dropdown li a:link, .dropdown li a:visited
{
width: 200px;
height: 44px;
display: block;
float: left;
margin: 0;
color: #ffffff;
background-color: #666666;
text-decoration: none;
text-indent: 12px;
line-height: 44px;
}
.dropdown li a:hover
{
background-color: #333333;
}
Notice that I declared a:link, instead of just a. I also declared a:visited since I want the visited state to be the same as the original state.
In the .dropdown entry, I simply defined a width for the list (ul) and made it display properly.
In the .dropdown li entry, I set the width and height of each list item, removed bullets, and set the display and float to make them display as a box. No padding. Just a box.
In the .dropdown li a:link, .dropdown li a:visited entry, what we are essentially doing is "filling" our list item boxes with a link, and it just so happens that we can define a link as a box as well. So I set the dimensions of the link to the exact same size as our li's (this is what makes the entire box clickable). Then set the display and float, and a background color, as well as a text color, or simply "color."
In order to get your text in the center, you should not use vertical-align. Use line-height, and set it to the same height as your li element. This will center your text in the box vertically.
To control where your text appears horizontally, set a text-indent property, use text-align. In this example, I used text-indent.
Finally, we define our a:hover state. It's important to remember that the only things you truly need to define here are any properties that actually change. In this case, the background color.
If you wanted to ad a state for the mouse down event, you could do something like:
.dropdown li a:active
{
background-color: #000000;
text-indent: 20px;
}
/////////// Edit////////////
Here's how you would use a single css styles for multiple menus or child menus.
html for 2 separate ul's:
<ul class="dropdown">
<li>Button 1</li>
<li>Button 1</li>
<li>Button 1</li>
</ul>
<ul class="dropdown">
<li>Button 1</li>
<li>Button 1</li>
<li>Button 1</li>
</ul>
CSS: (stays the same, because you assign both of them to the class "dropdown."
.dropdown
{
width: 200px;
display:block;
margin: 200px auto 0 auto;
list-style-type: none;
padding: 0;
}
.dropdown li
{
width: 200px;
height: 44px;
display: block;
float: left;
margin: 1px 0 0 0;
list-style-type: none;
padding: 0;
}
.dropdown li a:link, .dropdown li a:visited
{
width: 200px;
height: 44px;
display: block;
float: left;
margin: 0;
color: #ffffff;
background-color: #666666;
text-decoration: none;
text-indent: 12px;
line-height: 44px;
}
.dropdown li a:hover
{
background-color: #333333;
}
If you want to apply your styles for a child ul, like this:
<ul class="dropdown">
<li>Button 1
<ul>
<li><a href="#">subButton 1</li>
<li><a href="#">subButton 2</li>
<li><a href="#">subButton 3</li>
</ul>
</li>
<li>Button 1</li>
<li>Button 1</li>
</ul>
You simply append your styles to include the child ul and child ui li, as follows; where .dropdown actually represents your first ul. So .dropdown(ul) --> li (list item in "dropdown" unordered list) --> ul (ul inside dropdown li) --> li (li inside dropdown ul li ul)
Sorry if that sounds confusing, but in other words, if you didn't assign a class to your parent ul at all, it would be ul li ul li to access a list item inside a child list.
.dropdown, dropdown li ul
{
width: 200px;
display:block;
margin: 200px auto 0 auto;
list-style-type: none;
padding: 0;
}
.dropdown li, .dropdown li ul li
{
width: 200px;
height: 44px;
display: block;
float: left;
margin: 1px 0 0 0;
list-style-type: none;
padding: 0;
}
.dropdown li a:link, .dropdown li a:visited, .dropdown li ul li a:link, .dropdown li ul li a:visited
{
width: 200px;
height: 44px;
display: block;
float: left;
margin: 0;
color: #ffffff;
background-color: #666666;
text-decoration: none;
text-indent: 12px;
line-height: 44px;
}

http://www.webcredible.co.uk/user-friendly-resources/css/internet-explorer.shtml
^This has helped me on many occasions to find work-arounds for IE.

Related

Responsive Menu style using CSS Display the dropdown on hover

So I have looked at lots of other examples and I still can't figure out what I am missing. The menu works great except when I look at it in phone mode the menu does not drop on the hover. Can you help?
THE CSS code
/*Strip the ul of padding and list styling*/
#menu ul {
list-style: none;
width: 100%;
margin: 0;
padding: 0;
float: left;
background: #645565;
font-size: 1em;
}
/*Create a horizontal list with spacing*/
#menu li {
float: left;
position: relative;
display: inline-block;
}
/*Style for menu link*/
#menu li a {
display: block;
line-height: 3em;
font-family: Trebuchet MS, Verdana, Arial, sans-serif;
font-size: 0.95em;
color: #BFA877;
text-decoration: none;
padding: 0 0.9em;
}
/*Hover over text upper*/
#menu li:hover > a {
color: #FFF6D6;
}
/*Displays dropdown*/
#menu ul li:hover > ul {
display: block;
}
/*Hide dropdown links until they are needed*/
#menu li ul {
display: none;
position: absolute;
}
/*Display the dropdown on hover*/
.dropdown:hover .dropdown-content {
display: block;
}
/*Style "show menu' label button and hide it by default*/
.dropdown{
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-decoration: none;
color: #FFF6D6;
background: #645565;
text-align: center;
padding: 10px 0;
display: none;
z-index: 1;
}
/*Hide checkbox*/
input[type=checkbox] {
display: none;
}
/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked ~ #menu {
display: block;
}
/*==========MOBILE===========*/
/*Responsive Styles*/
#media screen and (max-width: 768px){
/*Make dropdown links appear inline*/
#menu ul {
position: static;
display: none;
}
/*Create vertical spacing*/
#menu li {
margin-bottom: 1px;
}
/*Make all menu links full width*/
#menu ul li, li a {
width: 100%;
}
/*Display "show menu" link*/
.dropdown {
display: block;
}
}
THE HTML code
<div id="container">
<label for="dropdown" class="dropdown">Show Menu</label>
<input type="checkbox" id="dropdown" role="button">
<div id="menu">
<ul>
<li>Home</li>
<li>
Massage & Spa
<ul class="dropdown-content">
<li>Massage Treatment</li>
<li>Balancing Energies</li>
<li>Signature Massage Treatment</li>
<li>Thai Massage</li>
<li>Signature Spa Treatment</li>
<li>List of Treatments & Rates</li>
</ul>
</li>
<li>Well Being</li>
<li>
About Us
<ul class="dropdown-content">
<li>Richard Davenport</li>
<li>Katrine Dannieu</li>
<li>Beth Drake</li>
<li>Daniel</li>
<li>Ciciely</li>
<li>Ish</li>
<li>Elizabeth</li>
<li>Tyler</li>
<li>Julia</li>
<li>Asley</li>
<li>Sandra</li>
</ul>
</li>
<li>
To Know
<ul class="dropdown-content">
<li>How to prepare for your massage</li>
<li>The right pressure during your Massage</li>
<li>How often should you get a massage?</li>
<li>Here is why you should book your next appointment ASAP</li>
<li>What is Rieki Who can learn</li>
<li>Dr. Oz and the healing power of Reiki</li>
</ul>
</li>
<li>Location</li><li>
Contact</li><li>
Calendar</li><li>
Events</li><li>
Links</li>
</ul>
</div>
</div><!-----End of container----->
Phones don't support the hover action!

Difficulty creating CSS menu with multiple columns

I've read through some similar threads on this site and found some helpful tips, but I'm still having difficulty getting columns to work correctly in my CSS drop down menu. The test site is here: http://iphonebuy-host1.gaiahost.net/index.html
In part I'm using ideas from method 4 in this article - http://alistapart.com/article/multicolumnlists - however this is for XHTML and I'm using HTML, maybe that's causing my issue?
The main thing is the list items in the second column don't stick to the bottom of the header. According to the referenced article, setting a negative margin on .reset is supposed to bring the entire second column up where I want it, but only the header (which has .reset applied to it) is moving up.
I should say that you probably have to view my menu in Firefox to see what I'm talking about - so far it's even more messed up in Safari and I haven't even tried IE or Chrome.
CSS
/** top navigation menu **/
.topnav {
list-style: none;
background-color: #FFF;
font: 1.313em arial, sans-serif;
color: #0071BC;
margin: -1.8em 0 1.2em 25em;
text-align: center;
}
.topnav li {
position: relative;
display: inline;
padding: 0 .5em 0 .5em;
border: none;
}
.topnav a {
display: inline-block;
}
/** for drop-down menu **/
.topnav li ol {
background: #fff;
list-style: none;
position: absolute;
width: 15.5em;
font: .8em arial, sans-serif;
padding: 0 1em .5em .5em;
margin-top: -.1em;
left: -9999px;
z-index: 200;
-webkit-border-radius: 0 0 .5em .5em;
-moz-border-radius: 0 0 .5em .5em;
border-radius: 0 0 .5em .5em;
-webkit-box-shadow: 0 3px 2px 1px #ccc;
-moz-box-shadow: 0 3px 2px 1px #ccc;
box-shadow: 0 3px 2px 1px #ccc;
}
.topnav li li h1 {
font: bold 1.2em arial, sans-serif;
white-space: nowrap;
margin: .5em 0 .5em 0;
}
.topnav li li h2 {
font: 1em arial, sans-serif;
white-space: nowrap;
}
.topnav li li a {
white-space: nowrap;
display: block;
}
.topnav li: hover ol {
left: 0;
margin-left: -.9em;
}
.topnav li: hover a {
color: #99CCCC;
}
.topnav li: hover ol a {
color: #0071BC;
}
.topnav li: hover ol a: hover {
color: #99CCCC;
}
.topnav li li.column1 {
margin-left: 0em;
width: 6.8em;
float: left;
line-height: 1.5em;
}
.topnav li li.column2 {
margin-left: 10em;
width: 4em;
float: left;
line-height: 1.5em;
}
.topnav li li.reset {
margin-top: -10.8em;
}
HTML
<div class="topnav">
<ol>
<li>Home</li>
<li>Get Quote
<ol>
<li class="column1"><h1>Select phone:</h1></li>
<li class="column1"><h2>CDMA</h2></li>
<li class="column1">3GS 8GB</li>
<li class="column1">3GS 16GB</li>
<li class="column1">4 8GB</li>
<li class="column1">4 16GB</li>
<li class="column1">4S 16GB</li>
<li class="column1">4S 32GB</li>
<li class="column2 reset"><h2>AT&T GSM</h2></li>
<li class="column2">3GS 8GB</li>
<li class="column2">3GS 16GB</li>
<li class="column2">4 8GB</li>
<li class="column2">4 16GB</li>
<li class="column2">4S 16GB</li>
<li class="column2">4S 32GB</li>
</ol>
</li>
<li>About</li>
</ol>
</div>
The code in the question floats the list items. The method 4 approach that it's based on doesn't. That one change prevents the approach from having the chance to work as intended.
In a case like this, it's best to either stay entirely consistent with the approach, or go in an completely different direction and do not imitate it at all. Getting caught in the middle -- inconsistently following the approach -- is likely to cause the most trouble.
Split the HTML into bite-sized chunks
You'll have a far easier time styling this if you change the HTML. Instead of putting everything into a single list and splitting the list up into 2 columns, try splitting the HTML into 2 separate lists.
It may require adding a few wrapper divs as well. Something like the following:
<div class="topnav">
<ul>
<li>Home</li>
<li>Get Quote
<div class="dropdown">
<h1>Select phone:</h1>
<div class="columns clearfix"> <!-- add a reliable clearfix -->
<div class="column1"> <!-- floated left -->
<h2>CDMA</h2>
<ul>
<li>3GS 8GB</li>
<li>3GS 16GB</li>
...
</ul>
</div>
<div class="column2"> <!-- floated left -->
<h2>AT&T GSM</h2>
<ul>
<li>3GS 8GB</li>
<li>3GS 16GB</li>
...
</ul>
</div>
</div>
</div>
</li>
<li>About</li>
</ul>
</div>
Splitting the related parts of the dropdown into separate HTML elements gives you more flexibility with styling it.
And semantically, HTML of this sort is much better, because the h1 and h2 tags aren't being treated as if they're the same type of content as the specific models of phone. That helps with SEO and accessibility.
Use The following CSS
.topnav {
list-style:none;
background-color:#FFF;
font:1.313em arial, sans-serif;
color:#0071BC;
margin:-1.8em 0 1.2em 25em;
text-align:center;
}
.topnav li {
position:relative;
display:inline;
padding:0 .5em 0 .5em;
border:none;
}
.topnav a {
display:inline-block;
}
.topnav li ol {
background:#fff;
list-style:none;
position:absolute;
width:15.5em;
font:.8em arial, sans-serif;
padding:0 1em .5em .5em;
margin-top:-.1em;
left:-9999px;
z-index:200;
-moz-border-radius:0 0 .5em .5em;
-webkit-border-radius:0 0 .5em .5em;
border-radius:0 0 .5em .5em;
-moz-box-shadow: 0 3px 2px 1px #ccc;
-webkit-box-shadow: 0 3px 2px 1px #ccc;
box-shadow: 0 3px 2px 1px #ccc;
}
.topnav li li {
}
.topnav li li h1 {
font:bold 1.2em arial, sans-serif;
white-space:nowrap;
margin:.5em 0 .5em 0;
}
.topnav li li h2 {
font:1em arial, sans-serif;
white-space:nowrap;
}
.topnav li li a {
white-space:nowrap;
display:block;
}
.topnav li:hover ol {
left:0;
}
.topnav li:hover a {
color:#99CCCC;
}
.topnav li:hover ol a {
color:#0071BC;
}
.topnav li:hover ol a:hover {
color:#99CCCC;
}
.topnav li li.column1 {
margin-left: 0em;
width:6.8em;
float:left;
line-height:1.5em;
}
.topnav li li.column2 {
/*margin-left:10em;*/
width:4em;
float:left;
line-height:1.5em;
}
.topnav li li.reset {
margin-top:-10.8em;
}
And The HTML
<div class="topnav">
<ol>
<li>Home</li>
<li>Get Quote
<ol>
<li class="column1"><h1>Select phone:</h1></li>
<div style="width:130px;height:auto;float:left">
<li class="column1"><h2>CDMA</h2></li>
<li class="column1">3GS 8GB</li>
<li class="column1">3GS 16GB</li>
<li class="column1">4 8GB</li>
<li class="column1">4 16GB</li>
<li class="column1">4S 16GB</li>
<li class="column1">4S 32GB</li>
</div>
<div style="width:130px;height:auto;float:left">
<li class="column2"><h2>AT&T GSM</h2></li>
<li class="column2">3GS 8GB</li>
<li class="column2">3GS 16GB</li>
<li class="column2">4 8GB</li>
<li class="column2">4 16GB</li>
<li class="column2">4S 16GB</li>
<li class="column2">4S 32GB</li>
</div>
</ol>
</li>
<li>About</li>
</ol>
</div>
What I did over here is, put column1 inside a division and column 2 on a different division. Hope this will solve your problem. Thank you.

Nav bar submenu container: How to cope with long list-item overflow by wrapping?

I have a hosted blog and I'm modifying templates, however the code for adding submenus to the nav bar is straight out of this great tutorial:
http://www.devinrolsen.com/pure-css-horizontal-menu/
I also read this answer to the question on Stackoverflow http://bit.ly/16KQN0M
But if it's a matter of clearing a float, I can't see where to put "clear: both;" in the HTML (at least, nothing I've tried has worked).
And changing #dropnav li li to display: block; doesn't work either. There must be something I'm missing ...
Here is the CSS. The classes at the top are related to the template but I'll leave them in as they might be relevant.
<style type="text/css">
/* Derived from http://www.devinrolsen.com/pure-css-horizontal-menu/ */
#pagebody { position: inherit !important; width: 100%; }
#pagebody-inner { position: inherit !important; width: 100%; }
#alpha, #beta, #gamma, #delta {
display: inline;
position: inherit !important;
float: left;
min-height: 0px;
}
#dropnav
{height:31px;}
#dropnav ul
{margin:0px; padding:0px;}
#dropnav ul li
{display:inline; float:left; list-style:none; position: relative; height:31px;
width: 175px; }
#dropnav li a
{color:#efe1ca; font-family:'Roboto Condensed', sans-serif; font-weight:400;
font-size:12px; }
#dropnav li a:hover
{color:#fff; text-decoration: none;}
#dropnav li ul
{margin: 0px; padding: 0px; display: none; position: absolute; z-index: 99; top:
31px; background-color: #f6f3cb; width: 250px;}
#dropnav li:hover ul
{display:block; width:250px; }
#dropnav li li
{list-style:none; display:list-item; font-size:100%;}
#dropnav li li a
{color: #7a3535; text-decoration:underline; width: 250px; font-size: 16px; }
#dropnav li li a:hover
{color:#000000; text-decoration:none; }
li#main {padding: 0px;} /* Sets the padding of items in the main menu */
</style>
And HTML markup, with titles removed:
<div id="main">
<div id="dropnav">
<ul>
<li class="nav-list-item">-------
<ul id="subnav">
<li style="width:180px;">-------</li>
<li style="width:180px;">-------</li>
<li style="width:180px;">-------</li>
<li style="width:180px;">-------</li>
</ul>
</li>
<li class="nav-list-item">-------
<ul id="subnav">
<li style="width:180px;">-------</li>
<li style="width:180px;"><a href="">-------</li>
</ul>
</li>
</ul>
</div>
</div>

Positioning Nested List in CSS on Hover

I am creating a center, nested navigation menu, and am trying to use pure CSS. See a working demo HERE: http://jsfiddle.net/jenstechs/MKtTN/2/
HTML:
<nav>
<ul id="primary">
<li>Link One</li>
<li>Link Two</li>
<li>Link Threee
<ul class="secondary">
<li>Services One</li>
<li>Services Two</li>
</ul>
</li>
<li>Link Four</li>
<li>Link Fiiiiive</li>
</ul>
</nav>
CSS:
nav {
margin:15px auto 10px auto;
width:100%;
}
nav ul#primary {
width: 100%;
padding: 10px 0;
margin: 0;
background-color: #FFF;
text-align: center;
}
nav ul#primary>li {
display: inline;
padding:5px 0;
margin-left:0;
}
nav ul#primary>li>a {
padding: 0px 30px;
margin-right:-6px;
color: #000;
text-decoration: none;
border-right:2px solid #999;
}
nav ul#primary>li>a:hover,
nav ul#primary>li.active>a {
background-color: #900;
color:#FFF;
padding-top:10px;
padding-bottom:25px;
}
nav ul#primary>li:first-child a {
border-left:2px solid #999;
}
ul.secondary {
padding-top:0;
position:absolute;
display:none;
}
ul.secondary li {
}
nav ul#primary li:hover ul.secondary {
display:block;
}
nav ul#primary li:hover ul.secondary li {
}
ul.secondary li a {
display:block;
width:7em;
color:#FFF;
background-color:#900;
font-size:0.8em;
text-decoration:none;
text-align:left;
line-height:1.4em;
border-bottom:1px solid #FFF;
}
ul.secondary li:last-child a {
border-bottom:0;
}
ul.secondary li a:hover {
color:#DDD;
}
​
I also have a minimal reset, the only styles it has on lists is a few default margins.
Since this is a centered navigation bar, the CSS I'm using is inline, not floated. So I have no idea what to put in the CSS to position the sub-menu actually underneath its parent element. Most examples I've seen have position:absolute but that seems to keep it at the left. I've tried various methods of hiding and showing (display:, left:) but can't seem to find that magic combination.
Here I only have the one sub-list, actually, but what if I had sublists for all of them?
Thanks for any tips or links to examples...
To make position: absolute elements position relative to their parent, make the parent position: relative.
In your case:
nav ul#primary>li {
display: inline;
padding:5px 0;
margin-left:0;
position: relative;
}
Demo: http://jsfiddle.net/MKtTN/3/

Make <li> fit the width of the <ul> using CSS

I'm trying to make the <li> fit the width of the <ul> but even with width:auto it doesn't work at all, and I don't know why. I tried to use display:inline-block but this is the same. I don't know how many tabs I will have so this is why I am not using a percentage directly.
I would like to display the list inline when I display the page on a desktop and display one li per line when I am on a smartphone (with media queries).
I have this:
<ul id='menu'>
<li class="button"><a class='current' href='http://'>Home</a></li>
<li class="button"><a href='http://'>Products</a></li>
<li class="button"><a href='http://'>Support</a></li>
<li class="button"><a href='http://'>Contact</a></li>
<li class="button"><a href='http://'>Contact</a></li>
</ul>
and my CSS looks like this:
ul#menu
{
margin:0;
padding:0;
list-style-type:none;
width:100%;
position:relative;
display:block;
height:30px;
font-size:12px;
font-weight:bold;
font-family:Arial, Helvetica, sans-serif;
/*border-bottom:1px solid #000000;
border-top:1px solid #000000;*/
}
li.button {
background:transparent url(../images/nav_bg.png) repeat-x top left;
height:30px;
width:auto;
}
ul#menu li
{
display:inline-block;
margin:0;
padding:0;
width:auto;
}
ul#menu li a
{
display:inline-block;
color:#999999;
text-decoration:none;
font-weight:bold;
padding:8px 20px 0 20px;
width:auto;
}
ul#menu li a:hover
{
color:#FFFFFF;
height:22px;
background:transparent url(../images/nav_bg.png) 0px -30px no-repeat;
}
ul#menu li a.current
{
display:inline-block;
height:22px;
background:transparent url(images/nav_bg.png) 0px -30px no-repeat;
margin:0;
}
I've found this way to deal with single-line full-width ul where an undefined number of li elements need to be spaced out evenly:
ul {
width: 100%;
display: table;
table-layout: fixed; /* optional */
}
ul li {
display: table-cell;
width: auto;
text-align: center;
}
Basically, it emulates a table. Works in Gecko, Webkit, IE8+.
For IE7 and downwards you should use some inline-block hackery :)
JSFiddle
Since the li count can change, I can only think of accomplishing this with javascript/jquery as you suggested. Just divide 100 by the # of li's and set the width on each one.
var width = Math.floor(100 / $("ul#menu li").size());
$("ul#menu li").css('width', width + "%");
You will probably have to play with the width depending on padding and what not.
As a side note, If you haven't already, I recommend getting a tool like firebug, which will let you edit css and execute js on the fly. It is infinitely useful for fine tuning appearances.
If you want to fill the width of the <ul> with the five <li>s, you will have to give those <li>s a width of 20% each.
Note that you need to change some other details of the CSS if you want to make this work; e.g. with a display:inline-block you make the spaces between the <li> count, so the total width of the <ul> content will be more than 100% wide. I'd suggest removing the display:inline-block and giving them float:left.
Edit: Or do you want them to be distributed proportionally according to their contents? That would be a different challenge.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
body{
margin:0 auto;
}
.main{
width:650px;
border:1px solid red;
padding:5px;
}
ul {
padding:0;
margin:0;
width: 100%;
border-bottom: 0;
}
li{
display: table-cell;
width: 1%;
float: none;
border:1px solid green;
margin:2px;
padding:10px;
text-align:center;
}
</style>
</head>
<body>
<div class="main">
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
</ul>
</div>
</body>
</html>
#menu {
padding: 0;
margin: 0;
border: 1px solid red;
position: absolute;
}
#menu li {
list-style-type: none;
float: left;
position: relative;
padding-right: 10px;
}
#menu li a {
text-decoration: none;
}
<ul id="menu">
<li>1A1CASĂ </li>
<li>H1TML-CSS </li>
<li>J1VASCRIPT </li>
<li>PHP </li>
<li>TESTE </li>
<li>CONTACT </li>
</ul>
Wow stumbled upon a very old question here.
For anyone also seeing this and scrolling down here, in 2022 this is easily doable via flexbox.
#menu {
display: flex;
gap: 1rem;
}
li {
list-style-type: none;
}
<ul id='menu'>
<li class="button"><a class='current' href='http://'>Home</a></li>
<li class="button"><a href='http://'>Products</a></li>
<li class="button"><a href='http://'>Support</a></li>
<li class="button"><a href='http://'>Contact</a></li>
<li class="button"><a href='http://'>Contact</a></li>
</ul>
try below css:
style.css (line 87)
ul#menu li {
float: left;
margin: 0;
padding: 6px 0;
width: 11.1%;
}
style.css (line 113)
ul#menu li a.current {
background: url("images/nav_bg.png") no-repeat scroll 0 -30px transparent;
height: 22px;
margin: 0;
}
style.css (line 95)
ul#menu li a {
color: #999999;
font-weight: bold;
padding: 8px 20px 0;
text-decoration: none;
width: auto;
}
see screen shot:

Resources