水平<ul>菜单的中心对齐?</ul>

时间:2014-11-25 19:55:30

标签: html css

我在下面有这个代码,但当它显示在网页上时,水平菜单不会居中。你能解决这个问题吗?

&#13;
&#13;
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;
}
&#13;
<ul>
  <li><a href="#home">Home</a>
  </li>
  <li><a href="#about">About</a>
  </li>
  <li><a href="#contact">Contact</a>
  </li>
</ul>
&#13;
&#13;
&#13;

提前致谢!

1 个答案:

答案 0 :(得分:0)

您必须移除浮动,将display: inline-block设置为项目,将text-align: center设置为容器。

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>