我无法将<ul>置于div(nav)中

时间:2015-12-11 19:12:17

标签: html css

我正在尝试将<ul>菜单置于<nav>元素中心,但我无法理解为什么它不起作用。我该如何解决这个问题?

nav {
  background-color: #333;
  width: 100%;
  height: 50px;
  background: -webkit-linear-gradient(#333, #000);
  /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(#333, #000);
  /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(#333, #000);
  /* For Firefox 3.6 to 15 */
  background: linear-gradient(#333, #000);
  /* Standard syntax */
  text-align: center;
}

nav ul li {
  list-style: none;
  float: left;
  width: 120px;
  height: 50px;
  position: relative;
  text-align: center;
  line-height: 50px;
}

nav ul li a {
  text-decoration: none;
  color: #000000;
  width: 120px;
  height: 50px;
  display: block;
  text-align: center;
  line-height: 50px;
  background-color: #979797;
}

nav ul li a:hover {
  background-color: #252525;
  color: #FFFFFF;
}
<nav>
  <ul>
    <li><a href="index.html" class="current">Home</a>
    </li>
    <li><a href="cv.html">CV</a>
    </li>
    <li><a href="portofolio.html">Portofolio</a>
    </li>
    <li><a href="contact.html">Contact</a>
    </li>
  </ul>
</nav>

1 个答案:

答案 0 :(得分:1)

您可以将display:inline-block;提交给ul

ul {
  display: inline-block;
  margin: 0;
  padding: 0;
}

nav {
  background-color: #333;
  width: 100%;
  height: 50px;
  background: -webkit-linear-gradient(#333, #000);
  /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(#333, #000);
  /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(#333, #000);
  /* For Firefox 3.6 to 15 */
  background: linear-gradient(#333, #000);
  /* Standard syntax */
  text-align: center;
}
ul {
  display: inline-block;
  margin: 0;
  padding: 0;
}
nav ul li {
  list-style: none;
  float: left;
  width: 120px;
  height: 50px;
  position: relative;
  text-align: center;
  line-height: 50px;
}
nav ul li a {
  text-decoration: none;
  color: #000000;
  width: 120px;
  height: 50px;
  display: block;
  text-align: center;
  line-height: 50px;
  background-color: #979797;
}
nav ul li a:hover {
  background-color: #252525;
  color: #FFFFFF;
}
<nav>
  <ul>
    <li><a href="index.html" class="current">Home</a>
    </li>
    <li><a href="cv.html">CV</a>
    </li>
    <li><a href="portofolio.html">Portofolio</a>
    </li>
    <li><a href="contact.html">Contact</a>
    </li>
  </ul>
</nav>