• 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

    • I'm fine with a little reasonable promotion of Edge, but the degree which they do it right now I consider extremely unreasonable. 
    • Microsoft AI boss no longer believes that AI will replace human workers by David Uzondu Mustafa Suleyman, the head of Microsoft AI, recently took back his statements concerning white-collar jobs that he gave to the Financial Times in an interview made back in February, where he claimed that AI would replace office workers within 12 to 18 months. On Monday's episode of The Verge's Decoder, Suleyman recast the technology as more like a helpmate than a tool designed to take over your job. He explained that smaller office duties will "increasingly become digitized, automated" as people generate more digital materials. During the discussion, Suleyman emphasized a "very important distinction" between "tasks" and "jobs" to clarify his previous claims. He argued that his earlier comments only referred to individual actions that people perform at their desks. Suleyman used to work for DeepMind, the research lab he co-founded in 2010 alongside Demis Hassabis and Shane Legg, before he left in 2022 to establish Inflection AI and build an empathetic digital assistant. Microsoft hired him in March 2024 to lead its newly formed "Microsoft AI" division, placing him in charge of consumer products like Copilot, Bing, and Edge. His February comments also detailed plans for Microsoft to achieve self-sufficiency with a $140 billion infrastructure budget to train frontier models, predicting that creating a customized AI will soon feel like creating a podcast or a new blog: The 41-year-old is not the only AI executive who's softened his "AI will replace you" stance. OpenAI's CEO, Sam Altman, last month used X to push back against employment panic by arguing that his startup builds tools to assist humans rather than build replacements. He had previously garnered backlash by suggesting that many modern office roles that AI might replace did not qualify as "real work" in the first place, at least when you compare desk jobs to physical, historical labor like farming.
    • Adobe Acrobat Reader DC 2026.001.21662 by Razvan Serea Adobe Acrobat Reader DC software is the free, trusted standard for viewing, printing, signing, and annotating PDFs. Its the only PDF viewer that can open and interact with all types of PDF content – including forms and multimedia. It’s connected to Adobe Document Cloud – so you can work with PDFs on computers and mobile devices. Adobe Document Cloud is a revolutionary, modern and efficient way to get work done with documents in the office, at home or on-the-go. At the heart of Document Cloud is the all-new Adobe Acrobat DC, which will take e-signatures mainstream by delivering free e-signing with every individual subscription. Document Cloud includes a set of integrated services that use a consistent online profile and personal document hub. With Adobe Document Cloud, people will be able to create, review, approve, sign and track documents whether on a desktop or mobile device. Businesses will be able to take advantage of Document Cloud for enterprise which provides enterprise-class document services that integrate into systems of record such as CRM, HCM, CLM, and CMS, adding speed, efficiency and transparency to getting business done with documents. Adobe Acrobat Reader DC new feature highlights: Work with PDFs from anywhere with the new, free Acrobat DC mobile app for Android or iOS. Select functionality is also available on Windows Phone. Use the new Fill & Sign tool in your desktop software to complete PDF forms fast with smart autofill. Download the free Adobe Fill & Sign mobile app to add the same option to your iPad or Android tablet device. Save money on ink and toner when printing from your Windows PC. Store and access files in Adobe Document Cloud with 5GB of free storage. Get instant access to recent files across desktop, web, and mobile devices with Mobile Link. Sync your Fill & Sign autofill collection across desktop, web, and iPad devices. Adobe PDF Pack premium features includes: Convert documents and images to PDF files. Use your mobile device camera to take a picture of a paper document or form and convert it to PDF. Turn PDFs into editable Microsoft Word, Excel, PowerPoint, or RTF files. Combine multiple files into a single PDF (web only). Get signatures from others with a complete e-signature service. Send, track, and confirm delivery of documents electronically instead of using fax or overnight services (tracking not available on mobile). Store and access files online with 20GB of storage. Download: Adobe Acrobat Reader DC 64-bit | 719.0 MB (Freeware) Link: Adobe Acrobat Reader DC Home Page | Release Notes | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Meta will now use data from outside businesses to personalize AI responses by David Uzondu In an update that's rolling out globally (except in a handful of countries), Meta will use your data from outside businesses to personalize your AI responses and your primary feeds. Meta already utilizes your shopping activity to target ads, but the company now plans to expand this tracking to personalize other "parts of your experience" like feed algorithms and AI assistant chats. The company is replacing the two settings ("Your activity off Meta technologies" and "Activity from other businesses") that currently let you disconnect off-platform activity with a single, renamed setting called Activity from other businesses. If you don't want Meta to manipulate your feed and AI responses using your outside history, you can just turn the Activity from other businesses setting off in your account settings. This toggle resides within your Accounts Center, applying your choice to every connected profile. Turning this off will not stop companies from sending your data to Meta. The company will still collect your web interactions, but it only uses them to train products, while still accessing external accounts you connect. When The Verge spoke to Meta spokesperson, Emil Vazquez, the representative said that this update will exclude several locations at launch including the European region, the UK, Brazil, Thailand, South Africa, Turkey, South Korea, Ecuador, Nigeria, and Kenya. The new update comes at a time when the social media giant is recovering from a major PR disaster involving generative AI. Last week, there was a huge security issue on Instagram where attackers figured out a way to exploit a prompt injection vulnerability. Hackers managed to trick Meta AI into handing over account ownership (even if the victim had 2FA enabled). Some of the affected accounts include the dormant Obama White House profile, cosmetics brand Sephora, the Chief Master Sergeant of the Space Force, and security researcher Jane Manchun Wong. Internally, the company also had to scale back plans on its Model Capability Initiative (MCI), an employee-monitoring program designed to train corporate AI models by recording worker keystrokes and screen activity, after employees raised privacy concerns and complained about severe battery life drain.
    • JetBrains is working to cut false positives in RustRover 2026.2 by David Uzondu Recently, JetBrains released the fifth EAP build of its dedicated IDE, RustRover 2026.2, bringing improvements like a Run gutter icon for criterion_main! macro benchmarking and a feature that alerts you when there are unused traits in your current scope. Now, the company is out with a blog post addressing one of the "most common" complaints from users: false positives. In RustRover, a false positive occurs when the editor incorrectly highlights something as an error even though the project compiles and runs successfully. This mismatch flags a gap between the IDE's internal intelligence and the actual compiler. When the editor flashes red warnings over perfectly valid code, developers lose trust in the tool, which stalls momentum. Traditionally, RustRover runs cargo check to detect compiler errors and warnings, but it also relies on its own code analysis engine to power real-time features. To provide quick feedback, this engine parses your source code into a syntax tree while inferring types and resolving names as you type. Because this engine must work on broken, half-written code and react instantly, its logic sometimes diverges from the compiler's, producing false positives that do not exist in the compiler's eyes. JetBrains said that it has a "dedicated task force" focused specifically on identifying and fixing false positives by analyzing user reports and examining large-scale open-source projects. To speed up this process, the team built an internal system modeled after Crater, the famous Rust project that compiles and runs tests for every single crate published on crates.io. This automated pipeline compares the diagnostics from RustRover's analysis with actual compiler output to catch discrepancies before they reach users, ensuring smoother workflows. RustRover, for those who're unaware, is a dedicated IDE designed specifically for Rust developers. It's been around for a couple of years now, providing features like built-in debugging via LLDB, seamless cargo integration, advanced macro expansion, and HTML support. JetBrains distributes the app under two licensing models: a paid commercial subscription and a free option for non-commercial use.
  • Recent Achievements

    • One Year In
      Primer1st earned a badge
      One Year In
    • Experienced
      JayZJay went up a rank
      Experienced
    • Reacting Well
      Sir_Timbit earned a badge
      Reacting Well
    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      512
    2. 2
      PsYcHoKiLLa
      229
    3. 3
      Edouard
      134
    4. 4
      ATLien_0
      87
    5. 5
      Steven P.
      80
  • Tell a friend

    Love Neowin? Tell a friend!