1

I have this code below, but when it shows up on the webpage, the horizontal menu isn't centered. Could you fix this?

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
li {
  float: left;
  position: relative;
}
a:link,
a:visited {
  display: block;
  width: 120px;
  font-weight: bold;
  color: #FFFFFF;
  background-color: #98bf21;
  text-align: center;
  padding: 4px;
  text-decoration: none;
  text-transform: uppercase;
}
a:hover,
a:active {
  background-color: #7A991A;
}
<ul>
  <li><a href="#home">Home</a>
  </li>
  <li><a href="#about">About</a>
  </li>
  <li><a href="#contact">Contact</a>
  </li>
</ul>

Thanks in advance!

emmanuel
  • 9,607
  • 10
  • 25
  • 38
JDS404
  • 65
  • 1
  • 1
  • 9

1 Answers1

0

You have to remove float, set display: inline-block to the items and text-align: center to container.

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  text-align: center;
}
li {
  /* float: left; */
  display: inline-block;
  position: relative;
}
a:link,
a:visited {
  display: block;
  width: 120px;
  font-weight: bold;
  color: #FFFFFF;
  background-color: #98bf21;
  text-align: center;
  padding: 4px;
  text-decoration: none;
  text-transform: uppercase;
}
a:hover,
a:active {
  background-color: #7A991A;
}
<ul>
  <li><a href="#home">Home</a>
  </li><!--
  --><li><a href="#about">About</a>
  </li><!--
  --><li><a href="#contact">Contact</a>
  </li>
</ul>
emmanuel
  • 9,607
  • 10
  • 25
  • 38