Search code examples
htmlcsscenternav

My navigation menu isn't completely centered for some reason


My navigation menu is always slightly off to the right from the center point.

like such> https://i.sstatic.net/KELYB.png

#nav {
  display: block;
  text-align: center;
  width: 100%;
  margin-right: auto;
  margin-left: auto;
  background-color: yellow;
}
#nav li {
  display: inline-block;
  padding: 1% 2% 1% 2%;
  list-style: none;
  background-color: blue;
  width: 5%;
  min-width: 50px;
}
#nav li a {
  color: white;
  text-decoration: none;
}
<div id="nav">
  <ul>
    <li><a href="index">Home</a>
    </li>
    <li><a href="about">About</a>
    </li>
    <li><a href="contact">Contact</a>
    </li>
  </ul>
</div>


Solution

  • You have to reset default ul padding.

    #nav ul { 
      padding: 0;
    }
    

    body {
      margin: 0;
    }
    #nav {
      display: block;
      text-align: center;
      width: 100%;
      margin-right: auto;
      margin-left: auto;
      background-color: yellow;
    }
    #nav li {
      display: inline-block;
      padding: 1% 2% 1% 2%;
      list-style: none;
      background-color: blue;
      width: 5%;
      min-width: 50px;
    }
    #nav li a {
      color: white;
      text-decoration: none;
    }
    #nav ul {
      padding: 0;
    }
    <div id="nav">
      <ul>
        <li><a href="index">Home</a>
        </li>
        <li><a href="about">About</a>
        </li>
        <li><a href="contact">Contact</a>
        </li>
      </ul>
    </div>