Question

So I have the following CSS in place to display a horizontal navigation bar using:

.navigation ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.navigation li {
  float: left;
  margin: 0 1.15em;
  /*    margin: 0 auto;*/
}

.navigation {
  /*    width: auto;*/
  /*    margin: 0 auto;*/
  text-align: center;
}

My question is: how do I align the navigation bar centrally above the title?

Was it helpful?

Solution

Give your .navigation ul a width and use margin:0 auto;

.navigation ul 
{
  list-style: none;
  padding: 0;
  width: 400px;
  margin: 0 auto;
}

OTHER TIPS

Well, to use margin:0 auto on something, it must have a defined width. Probably the best workaround is:

ul li {
  display: inline;
  list-style-type: none;
}
ul {
  text-align:center;
}

There are few settings like float, margin which may affect this code to work properly. It works in IE7 too. I got this code from an article over at CSS Wizardry.

.navigation ul 
{
    list-style: none;
    padding: 0;
    margin: 0;
    text-align: center;/*make this change*/
 }
.navigation li
 {
    float: none;/*make this change*/
    display:inline;/*make this change*/
    margin: 0 1.15em;
 /* margin: 0 auto; */
 }
 .navigation li a {
    display:inline-block;/*make this change*/
 }
 .navigation 
 {
  /*width: auto;*/
  /*margin: 0 auto;*/
    text-align: center;
 }

You could set the <li>'s to be display: inline, then set text-align: center on the <ul>. Doing that, you can remove the float: left from the list items and you don't need to have a fixed width for the navigation bar as you would if you used margin: 0 auto.

<html>
  <head>
    <style>
      ul { 
        list-style: none;
        text-align: center;
      }

      li {
        display: inline;
        margin: 0 1.15em;
      }
    </style>
  </head>

  <body>
    <ul>
      <li>Option 1</li>
      <li>Option 2</li>
      <li>Option 3</li>
    </ul>
  </body>
</html>

This one works great with me! (if I'm correct: IE7+)

Fiddle: http://jsfiddle.net/fourroses666/zj8sav9q/3/

.nav{list-style:none; text-align:center;}
.nav ul{list-style:none;}
.nav li{display:inline;}
.nav a{text-decoration:none; font-size:20px; line-height:20px; display:inline-block;}

<nav class="nav" role="navigation" aria-label="main navigation">
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">Menu</a></li>
    <li><a href="#">Onze producten</a></li>
    <li><a href="#">Impressie</a></li>
    <li><a href="#">Media</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>
ul {
 display: inline-block;
 text-align: center;
}
.navigation ul 
{
  list-style-type: none;
  padding: 0px;
  width: 200px;
  margin: 0 auto;
}

If you want to keep using your floated LI in your code, you can use this:

JSFiddle: https://jsfiddle.net/Lvujttw3/

<style>
.wrapper {
    width: 100%;
    background-color:#80B5EB;
    text-align: center;
    }
.intWrapper {
    display: inline-block;
    }
.mainMenu {
    padding: 0;
    min-height: 40px;
    margin:auto;
    }
ul {
    list-style-type: none;
    }
ul li {
    float: left;
    font-size: 15px;
    line-height: 40px;
    }
ul li A {
    display: block;
    color: white;
    font-weight: bold;
    font-family: Arial;
    text-decoration: none;
    min-height: 40px;
    padding: 0 36px;
    }
</style>

<div class="wrapper">
    <div class="intWrapper"> 
        <ul class="mainMenu">   
            <li><a href="one.htm" style='background-color:red'>ITEM ONE</a>
            </li><li><a href="two.htm">ITEM TWO</a>
            </li><li><a href="three.htm" style='background-color:red'>ITEM THREE</a>        
            </li>   
        </ul></div>
    </div>
</div>
style="position: absolute; z-index: 1;"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top