• 0

Help with css <nav> <ul> <li> drop down menu alignm


Question

What I'm having difficulty with is that I want to make the menu do several things if it all possible:

1) Span the whole nav menu to the width of whatever the container div is.

2) Automatically space the list items in the top menu evenly without having to specify.

3) Wrap the text when required (only managed to achieve this so far by specifying width.)

4) When the menu item is a single word on a single line, align it vertically, not towards the top as it is now!

5) Ignoring the submenu items for the moment.

I've dumped the code here and you can see the current result:

http://jsfiddle.net/b2bUV/

css:


nav {
font: bold 12px Arial, Helvetica;
}

nav ul ul {
display: none
}

nav ul li:hover &gt; ul {
display: block;
}

nav ul {
background: #e6e6e6;
padding: 0px 0px ;
border-radius: 5px;
border-style: solid;
border-width: 1px;
border-color:b3b3b3;
list-style: none;
position: relative;
display: inline-table;
text-align: center;

}
nav ul:after {
content: ""; clear: both; display: block;
}

nav ul li {
float: left;
width: 85.8px;
height: auto;
padding-left: 5px;
padding-right: 5px;
}
nav ul li:hover {
background: #c3c3c3;
}
nav ul li:hover a {
color: #fff;
}

nav ul li a {
display: block; padding: 10px 0px 10px 0px; height: 30px;
color: #000; text-decoration: none;
}

nav ul ul {
background: #b3b3b3; border-radius: 0px; padding: 0;
position: absolute; top: 100%;
}
nav ul ul li {
float: none;
border-top: 1px solid #f2f2f2;
border-bottom: 1px solid #f2f2f2;
position: relative;
padding: 0px;
width: auto;
height: auto;
text-align: left;
}
nav ul ul li a {
padding: 0px 0px 0px 0px;
color: #fff;
}
nav ul ul li a:hover {
background: #757575;
}

nav ul ul ul {
position: absolute; left: 100%; top:0;
}

html:


&lt;nav&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;Computer Parts&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Desktops &amp; Servers&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Notebooks &amp; Laptops&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Software&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Gaming&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Accessories &amp; Peripherals&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Office Solutions&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Printer &amp; Inks&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Networking&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;More&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Sub-Menu Item&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;

13 answers to this question

Recommended Posts

  • 0

Hey bud.

Ha yeah it's a bit messy as it was "frankensteined" from something I'd done previously and some other tutorial I found.

What I wanna achieve is a menu with drop downs (not decided how I want the drop downs to behave/look yet so not too worried about those).

The menu is going to go into a fixed width div sized 1020px with 30px of padding either side. This leaves 960px for the full width of the menu bar.

HOWEVER...

Lets assume we want to make this as flexible as possible whereby menu items can be removed or added and the whole thing wont get broken (within reason). So I want the menu items to space themselves evenly across the menu bar and the text to wrap onto two lines where appropriate. I also really want to get the text to vertically align to the middle of the menu bar but the vertical-align: middle tag doesnt seem to achieve that.

Hope this is not too much to ask, I know you've helped me before and very much appreciate you stepping up to the post again.

Many thanks :)

  • 0

HOWEVER...

Lets assume we want to make this as flexible as possible whereby menu items can be removed or added and the whole thing wont get broken (within reason). So I want the menu items to space themselves evenly across the menu bar and the text to wrap onto two lines where appropriate. I also really want to get the text to vertically align to the middle of the menu bar but the vertical-align: middle tag doesnt seem to achieve that.

Hope this is not too much to ask, I know you've helped me before and very much appreciate you stepping up to the post again.

Many thanks :)

Without using javascript, or with it?

Without this is a huge PITA. In fact, I'm not sure there's even a way to get things to wrap at appropriate times without specifying a width, unless you're going to calculate string lengths.

  • 0

I positioned the items on the left, making them use the whole bar like at apple.com requires js or css that breaks when a additional menu item is added :/

Edit seems possible but require IE9.js to work in IE8 and lower, I can give you a link to it if you want (I love IE9.js for my own sites btw :p)

  • 0

Thanks seahorse! Thought I might have to resort to JS at some point would wanted to avoid it if at all possible as it's lost on me! I'll have to have a look at this later on / tomorrow and see what I can do when playing with it. Looks fantastic and more or less close to what I'm trying to achieve apart from these points:-

Trouble is that the bar wraps down onto another level if I attempt to implement it into the site. Bearing in mind there's only 960px width to work with. I wondered if rather than the list/navbar wrapping onto the next level, the actual text within the bar would wrap itself onto two or more lines instead. With evenly spaced menu items rather than ones of varying width? I hope this makes sense!

Gotta drive somewhere now but I'll hop back on here once i'm at my destination!

Many thanks!

  • 0

Trouble is that the bar wraps down onto another level if I attempt to implement it into the site. Bearing in mind there's only 960px width to work with. I wondered if rather than the list/navbar wrapping onto the next level, the actual text within the bar would wrap itself onto two or more lines instead. With evenly spaced menu items rather than ones of varying width? I hope this makes sense!

Maths. :laugh:

Once you get to math, it's easier to use javascript. 960 / # of li elements will get you a width for each, or you can go further and get the string length of each entry and then figure out some loose calculation based on the font to size them, so that "Desktops and Laptops" doesn't get the same width as "Mac". You could also use this to adjust the line height for single line items so they line up with the middle of double line items.

How flexible does this need to be, BTW? Developing a menu like this for a completely open ended CMS theme is usually a nightmare.

  • 0

Heres a pure css version ;) : http://jsfiddle.net/b2bUV/121/

And yes this does require IE9.js to work right on IE8 :p

If the menu items with long text are shortened too much consider changing the 12px font size to 11px this already makes a huge difference ^^

  • 0

Developing a menu like this for a completely open ended CMS theme is usually a nightmare.

That and dropdowns are really bad for touchscreen devices I'll try every possible option before I think about a dropdown menu on my own sites :p

  • 0

Thanks seahorse - it's getting there! Is there any way we can make the text wrap onto multiple lines rather than have it shortened as it is like in my original version or is that not possible??

I'm making an effort to go through the code and experiment but I fear I won't make much progress until tomorrow - currently playing with it via teamviewer on an ipad so as you can imagine it's not the ideal workstation right now!

Appreciate this :)

  • 0

Maths. :laugh:

Once you get to math, it's easier to use javascript. 960 / # of li elements will get you a width for each, or you can go further and get the string length of each entry and then figure out some loose calculation based on the font to size them, so that "Desktops and Laptops" doesn't get the same width as "Mac". You could also use this to adjust the line height for single line items so they line up with the middle of double line items.

How flexible does this need to be, BTW? Developing a menu like this for a completely open ended CMS theme is usually a nightmare.

It's just an exercise to see how flexible I can make it I suppose! Maths? I love Maths! Trouble is I've never touched javascript and wouldnt know where to start! :p - Understand it may be the best/only solution though!

  • 0

It's just an exercise to see how flexible I can make it I suppose! Maths? I love Maths! Trouble is I've never touched javascript and wouldnt know where to start! :p - Understand it may be the best/only solution though!

http://jsfiddle.net/b2bUV/124/

Just to get you started, using jQuery.

This topic is now closed to further replies.
  • Posts

    • Dragon's Dogma 2: Dark Arisen expansion to bring snowy region, new updates also coming by Pulasthi Ariyasinghe Capcom had a surprise waiting for Dragon's Dogma fans today in the Nintendo Direct presentation. The company revealed an expansion for the second installment with a name that should be familiar to series veterans. Coming later this year, Dragon's Dogma 2: Dark Arisen is promising a massive new region to explore, new monsters, fresh skills to learn, and more. The studio says players will be heading to the Northern region of the world, named Norgan, to find new secrets about an undying "Fallen Dragon." There will be forgotten relics that the protagonist can find to unlock fresh weapons and skills the expansion is introducing. Players will also be able to find mysterious equipment from a previous Arisen as a part of the expansion, all part of 12 Lost Rites Dungeon Challenges they must complete to gain access. In Neowin's own review, I found Dragon's Dogma 2 to be an impressive RPG when it launched back in 2024, giving the title an 8.5/10 for its class variants, companion system, and immersive exploration. "Once a prosperous region of the kingdom of Vermund, it was abandoned many years ago for reasons unknown," says Capcom about the new region. "Long has it been since any soul traveled its paths. Blanketed in heavy snow, these frigid lands are home to savage hordes and creatures of unbelievable power. Those who are capable of vanquishing such fearsome foes, or those who possess a keen eye for exploration, will find themselves rewarded with powerful relics." Dragon’s Dogma 2: Dark Arisen expansion launches on October 9, 2026, with a $29.99 price tag. Ahead of the expansion release, Capcom is also planning to release two free updates to the base game. The first will land tomorrow, June 10, bringing more accessible fast travel with an Eternal Ferrystone and other quality-of-life adjustments. The second update will land sometime in August, aiming to improve frame rates, add more save slots, and bring even more community-requested adjustments. This expanded Dark Arisen edition is also launching on the Nintendo Switch 2 on the same day the content comes to PC, Xbox Series X|S, and PlayStation 5.
    • Classic themes are just the colors on the bar like the olden days, if you use the image themes, it does fancy transparent backgrounds and it makes the elements of the app look like they are transparent bubbles. This sample image shows what it looks like.  
    • Good point, unfortunately. NextDNS has far more filters and workarounds than uBlock, and it's easy to implement.
  • Recent Achievements

    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
    • One Year In
      jojodbn earned a badge
      One Year In
    • One Month Later
      jojodbn earned a badge
      One Month Later
    • Week One Done
      jojodbn earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      524
    2. 2
      PsYcHoKiLLa
      231
    3. 3
      +Edouard
      124
    4. 4
      ATLien_0
      87
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!