Peter W
Peter W

Reputation: 75

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>

Upvotes: 1

Views: 95

Answers (2)

radiovisual
radiovisual

Reputation: 6468

You have unwanted padding to the left of your ul element.

This will fix it:

#nav ul { padding:0; }

Fidde here

Upvotes: 1

emmanuel
emmanuel

Reputation: 9615

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>

Upvotes: 1

Related Questions