• 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

    • You can disable the bloat on every browser. That's not the point. I will never use a browser of a shady company. I don't trust them at all. I can still find adblocking solutions than having to rely on a browser from a shady company. Every year they try something shady lol 2016: Brave Ad Replacement https://archive.is/W0k4j#selection-203.7-203.28 2016: pay-to-win Wikipedia clone into the default search engine list https://github.com/brave/browser-laptop/issues/5475 2018: Tom Scott and other creators noticed Brave was soliciting donations in their names without their knowledge or consent. https://www.reddit.com/r/brave...aims_that_brave_is_falsely/ 2020: Brave got caught injecting URLs with affiliate codes https://www.theverge.com/2020/...-crypto-privacy-ceo-apology 2021: Brave's TOR window was found leaking DNS queries https://www.zdnet.com/article/...n-addresses-in-dns-traffic/ 2022: Brave floated the idea of further discouraging users from disabling sponsored messages. https://github.com/brave/brave-browser/issues/22066 2023: Brave got caught installing a paid VPN service on users' computers without their consent. https://www.xda-developers.com...owser-installs-vpn-windows/ 2023: Brave got caught scraping and reselling people's data with their custom web crawler, which was designed specifically not to announce itself to website owners. https://stackdiary.com/brave-s...ghted-data-for-ai-training/ 2024: Brave gave up on providing advanced fingerprint protection, citing flawed statistics https://www.bleepingcomputer.c...tion-as-it-breaks-websites/ 2025: Brave staff publish an article endorsing PrivacyTests and say they "work with legitimate testing sites" like them. This article fails to disclose PrivacyTests is run by a Brave Senior Architect! https://brave.com/blog/adblock...esting-websites-harm-users/
    • Alpine Linux 3.24 released with support for COSMIC Desktop and other improvements by David Uzondu Alpine Linux 3.24 has been released with updated system packages, including Linux kernel 6.18 and Rust 1.96. The team also added IPv6 support to the system installer, and they introduced automatic serial console configuration for headless setups. System76's COSMIC desktop environment is now available in the community repo. System76 originally started building this DE because its developers found GNOME to be pretty limited. Plus, it did not help that with virtually every GNOME update, the changes broke System76's custom desktop extensions. As for system packages, the Alpine team moved GTK+ 3.0 from the main repository to the community repository due to its legacy status. py3-setuptools has been upgraded to version 82.0.0, while the old pkg_resources module has been completely dropped. The team also removed outdated packages that still relied on py3-six and GTK+ 2.0. In addition to that, libsoup 2 has been removed because the library was affected by multiple security vulnerabilities. If you're a GRUB user, the Alpine Team said that you must manually run the grub-install command with your specific device or EFI options right after upgrading your system, otherwise, your computer may fail to boot properly with the newly updated GRUB 2.14 bootloader. New installations of Alpine Linux now offer an optional path to a /usr-merged directory layout if you set the BOOTSTRAP_USR_MERGED environment variable to 1 before you execute the setup-disk command. If you already run an older installation, you can migrate manually by installing the merge-usr package and executing its binary as the root user. The team recommends this layout to align Alpine with modern Linux standards, though you should verify your custom scripts before making the switch. Alpine Linux is a pretty tiny (~5MB) Linux distro built around musl libc, BusyBox, and OpenRC. It's been around since 2005, comes with its own package manager called Alpine Package Keeper (APK), and is widely used in modern cloud computing and software deployment.
    • Instagram now lets you manually reorder posts on your profile grid by David Uzondu Instagram is finally rolling out the ability to customize your feed layout as you see fit by letting you reorder posts on your profile grid. This feature comes several months after the app introduced a tool that lets users rearrange photos and videos within a carousel post after it has already been published. To do that, people tap the three-dot menu in the top right corner of the post, select the edit option, and reorganize their slides. Now that Instagram has expanded the feature to your profile grid, you can organize your main page without deleting old uploads. To use the new system, you simply tap any picture on your grid and select the option to reorder. This action opens up a separate screen where you can freely drag your grid items around until you get your preferred aesthetic, and then you just hit the back button to save your changes. Instagram's Threads account posted that the system would reach accounts starting this week, so you might need to wait for the automatic update to hit your phone. https://www.threads.com/@instagram/post/DZVV_fyjjSW In other Instagram news, last week, people figured out that if you ask Meta's AI support assistant to hand over any Instagram account, the bot will actually hand it over (even if the victim's account had 2FA enabled). The security exploit involved the assistant accepting prompts from users and generating password reset links for unauthorized email addresses. Meta said that the issue has now been fixed, but this came after the issue affected several high-profile accounts, including @obamawhitehouse. Last month, the company finally rolled out paid subscription tiers for WhatsApp and other Meta social platforms after months of testing. WhatsApp Plus costs $2.99 a month and gives you custom themes, while Instagram Plus and Facebook Plus cost $3.99 a month for extra profile customization and story rewatch counters. Meta's also working on Meta One, a unified subscription service that contains options for heavy users of its servers who want more reach or advanced features. For instance, Meta One Essential ($14.99/mo) comes with a verified badge and impersonation protection. If you pay for Meta One Premium ($19.99/mo), you get deeper AI reasoning tools, whereas the Meta One Advanced ($49.99/mo) tier increases your search placement (on Facebook and Instagram) and visibility.
    • Hello mysterious lamborghiniv10, I was in Australia and... now I'm in the Netherlands. 
    • EU says Meta must restore rival chatbots' access to WhatsApp by Hamid Ganji The European Commission has ordered Meta to restore third-party AI chatbots’ access to WhatsApp after the tech giant decided to block them from operating on the popular messaging platform. After Meta banned rival AI chatbots from operating on WhatsApp, the European Commission launched an antitrust investigation to determine whether the company had abused its market dominance. As a result of Meta’s decision, third-party AI chatbots, including Microsoft’s Copilot and OpenAI’s ChatGPT, were prevented from operating on WhatsApp. At the time, Meta said it wanted to reserve the WhatsApp Business API for other types of businesses and did not allow rival chatbots to use it. This effectively prevented the WhatsApp ecosystem from being used to distribute rival chatbot services. However, the European Commission has now announced an interim measures decision requiring Meta to restore access to WhatsApp for rival general-purpose AI assistants on the same terms and conditions as before October 15, 2025. The Commission has also asked Meta to maintain that access until the antitrust investigation is concluded. The Commission argues that Meta has used its dominant market position to prevent rival AI chatbots from accessing the WhatsApp Business API. While Meta allowed rival services to return to WhatsApp by paying a fee, the European Commission still considers that arrangement to be a de facto access ban. According to EU antitrust chief Teresa Ribera, the fees introduced by Meta are so high that using WhatsApp is no longer economically sustainable for competitors. “It seems that Meta expects to leverage the vast reach and likely dominance of WhatsApp to benefit its own AI assistant and to foreclose rivals,” Ribera said. “We cannot let large digital incumbents leverage their dominance of the past to dictate who in Europe gets to compete and who gets to innovate in AI.”
  • 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!