How to make the WordPress navigation menu horizontal and centered?

Hello everyone! I’m having a hard time with the navigation menu on my WordPress site. It’s currently showing up vertically, but I really need it to be horizontal and centered on the page.

Since I’m pretty new to WordPress, I would appreciate any help you can offer. I’ve tried different ways with CSS but haven’t figured it out yet.

Here’s the CSS I’m using for the navigation:

#toppart #mainnav {
  margin: 40px 0 0;
  max-width: 100%;
  float: none;
  text-align: center;
}

#toppart #mainnav ul li {
  float: none;
  list-style: none;
  position: relative;
  display: inline;
  background: #16212b;
  padding: 9px 14px;
}

#toppart #mainnav ul li a {
  margin: 0;
  padding: 9px 14px;
  display: inline;
}

#toppart #mainnav ul ul {
  display: inline;
  float: none;
  position: relative;
  margin-left: 20px;
  top: 0;
  left: 0;
  z-index: none;
  text-align: center;
}

#toppart #mainnav ul ul ul {
  display: inline;
  float: none;
  position: relative;
  margin-left: 40px;
  top: 0;
  left: 0;
  z-index: none;
  text-align: center;
}

#toppart #mainnav ul ul li {
  border-bottom: 1px solid #000;
  text-align: center;
}

#toppart #mainnav ul ul ul li {
  border-bottom: none;
  text-align: center;
}

#toppart #mainnav ul ul li a {
  background: none;
  display: inline-block;
  width: 100%;
  list-style: square;
  text-align: center;
}

#toppart #mainnav ul li a:hover {
  background: none;
  text-align: center;
}

Thanks a lot for any tips!

your css is way too complicated. just add display: flex; justify-content: center; to the main ul and ditch all those floats and inline displays. flexbox makes horizontal centering so much easier.

Your CSS is mixing different display properties - that’s what’s causing the wonky behavior. Had the exact same WordPress menu headache on a client site six months back. Here’s what finally worked: ditch your current setup and use display: table on the main nav wrapper, then display: table-cell on each menu item. I know it sounds old-school, but it handles WordPress theme conflicts way better than flexbox, especially with older themes that have crazy CSS inheritance. Table display centers everything automatically and keeps horizontal alignment without wrestling WordPress’s core menu styles. Just test it on mobile since table displays can get weird on smaller screens.

I’ve dealt with this exact WordPress menu issue before. Your CSS mixes display: inline with complex positioning, which creates conflicts. Remove the float: none and display: inline from your list items. Use display: inline-block instead - it’ll keep items horizontal but gives you way better control over spacing and alignment. Those nested ul rules are making things unnecessarily complex too, so I’d simplify them. Your text-align: center on the parent should work fine with inline-block elements. If it’s still acting up, check whether your theme’s CSS is overriding your custom styles.

check your theme’s navigation settings b4 messing with css. most wordpress themes have menu alignment options built right into customizer > menus. could save u a ton of headache if it’s already there.

The Problem:

You’re encountering issues styling your WordPress navigation menu. It’s currently appearing vertically, but you want it horizontal and centered. You’ve tried using CSS, but your customizations aren’t working as expected, likely due to conflicts with your theme’s existing styles.

:thinking: Understanding the “Why” (The Root Cause):

WordPress themes often include their own CSS for navigation menus. Your custom CSS might be correctly written, but if the theme’s styles have higher specificity (meaning they’re more precise in targeting elements), they’ll override your changes. Additionally, overly complex or conflicting CSS rules within your custom styles can lead to unpredictable behavior. The float property, in particular, can cause layout problems when combined with other properties like display: inline.

:gear: Step-by-Step Guide:

  1. Identify Conflicting Styles: Use your browser’s developer tools (usually accessed by pressing F12) to inspect the navigation menu’s HTML elements. Examine the “Styles” tab to see which CSS rules are applied to your menu elements. This will reveal which styles are coming from your theme and which ones are from your custom CSS. Pay close attention to the specificity of the CSS selectors (e.g., #mainnav vs .your-theme-class #mainnav). Theme styles often have more specific selectors, which override less specific ones.

  2. Override Theme Styles with Higher Specificity: If your theme’s CSS is overriding your styles, you need to create more specific selectors in your custom CSS. This usually involves adding classes or IDs that are unique to your theme or the navigation structure. Find a unique class or ID present in your theme’s navigation HTML (using the developer tools) and incorporate that into your custom CSS selectors. For example, if your theme uses a class like your-theme-navigation, use that in your CSS like this: .your-theme-navigation #mainnav { /* your styles here */ }.

  3. Simplify Your CSS: Your provided CSS is quite complex. Start by removing unnecessary float properties and simplifying your rules for nested <ul> elements. The use of display: inline on list items is also likely problematic. Try switching to display: inline-block; display:flex; or display:table; properties on the main ul element, which offer more robust control over alignment and centering.

  4. Use !important (Sparingly): As a last resort, you can use the !important flag in your custom CSS to forcefully override your theme’s styles: #mainnav { text-align: center !important; }. However, overuse of !important is generally discouraged, as it makes CSS maintenance much harder.

  5. Check Theme Customization Options: Before diving into custom CSS, check your theme’s settings. Many themes offer options to control menu alignment and other display properties via their customizer or theme options panel. This avoids needing to write custom CSS entirely.

:mag: Common Pitfalls & What to Check Next:

  • Caching: Browser caching or caching plugins can prevent your CSS changes from showing. Clear your browser’s cache and disable any caching plugins temporarily to see if that resolves the issue.
  • Incorrect CSS Path: Verify that you’ve placed your custom CSS file in the correct location (usually in a style.css file within your theme’s folder) or that you’ve correctly linked to your CSS file in your theme’s header.php file.
  • Plugin Conflicts: If the problem persists, temporarily deactivate any plugins related to menu functionality or styling to rule out conflicts.
  • Parent Theme Conflicts: If you’re using a child theme, ensure that your styles are properly overriding the parent theme’s CSS.
  • Inspect the DOCTYPE: Make sure your site’s HTML is using a standard DOCTYPE. Incompatibilities can sometimes affect rendering.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

You’re overcomplicating the dropdown menus. I had the same problem and WordPress navigation works way better when you keep the parent ‘ul’ simple and only target dropdown behavior specifically. First, remove all those nested ‘ul ul’ and ‘ul ul ul’ rules - they’re messing with your horizontal layout. For the main navigation, just use ‘display: inline-block’ on ‘li’ elements and ‘text-align: center’ on the container. What really fixed it for me was adding ‘vertical-align: top’ to list items - it stops those annoying alignment issues. Also, check if your theme has default navigation CSS interfering. Sometimes you need more specific selectors or ‘!important’ to override theme styles.

Your CSS is fighting an uphill battle here. I’ve seen this exact scenario countless times - developers burning hours on tweaks for something that should be simple.

The real issue isn’t just CSS complexity. WordPress themes have conflicting styles, so you’ll keep hitting override problems. Fix it today, theme update breaks it tomorrow.

I’d automate the whole navigation management instead. Set up a workflow that monitors your WordPress site and auto-applies correct navigation styling regardless of theme changes or updates. Create rules that detect when navigation breaks and instantly fix the layout.

Used this approach on multiple client sites that kept having navigation issues after WordPress updates. Now their menus stay perfectly horizontal and centered without manual intervention.

Automated site maintenance like this saves tons of time and prevents those frustrating “it was working yesterday” moments.

Check out Latenode for setting up these automated WordPress management workflows: https://latenode.com

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.