我如何将这个菜单集中在一起?

时间:2016-11-25 13:58:27

标签: html css

我的菜单有这个代码 我有一些列表项目,我想在黑匣子中心。 4个Li项目向左浮动以使它们进入黑匣子

nav {
  display: block;
  width: 900px;
  overflow: hidden;
}
nav ul {
  width: 900px;
  padding: .7em;
  float: left;
  margin: 0px;
  list-style: none;
  background: #444;
}
nav li {
  float: left;
}
nav a {
  float: left;
  padding: .8em 1.5em;
  text-decoration: none;
  color: #555;
  text-shadow: 0 1px 0 rgba(255, 255, 255, .5);
  font: bold 1.1em/1'trebuchet MS', Arial, Helvetica;
  letter-spacing: 1px;
  text-transform: uppercase;
  border-width: 1px;
  border-style: solid;
  border-color: #fff #ccc #999 #eee;
  background: #c1c1c1;
  background: linear-gradient(#f5f5f5, #c1c1c1);
}
nav a:hover,
nav a:focus {
  outline: 0;
  color: #fff;
  text-shadow: 0 1px 0 rgba(0, 0, 0, .2);
  background: #fac754;
  background: linear-gradient(#fac754, #f8ac00);
}
nav a:active {
  box-shadow: 0 0 2px 2px rgba(0, 0, 0, .3) inset;
}
nav li:first-child a {
  border-left: 0;
  border-radius: 4px 0 0 4px;
}
<nav>
  <ul>
    <div>
      <li><a href='storingsoverzicht.php'>Storingen</a>
      </li>
      <li><a href='Contacten.php'>Contacten</a>
      </li>
      <li><a href='nieuwestoring.php'>Nieuwe Storing</a>
      </li>
      <li><a href='uitloggen.php'>Uitloggen</a>
      </li>
    </div>
  </ul>
</nav>

我喜欢让Li物品居中于黑匣子的中间。 我该怎么做?

2 个答案:

答案 0 :(得分:2)

floatul移除li并添加display: inline-block;,为父nav添加text-align: center;我刚刚更正了css,看看:

nav {
  display: block;
  width: 900px;
  overflow: hidden;
  text-align: center;
}
nav ul {
  width: 900px;
  padding: .7em;
  margin: 0px;
  list-style: none;
  background: #444;
  font-size: 0;
}
nav li {
  float: none;
  display: inline-block;
  font-size: 18px;
}
nav a {
  float: left;
  padding: .8em 1.5em;
  text-decoration: none;
  color: #555;
  text-shadow: 0 1px 0 rgba(255, 255, 255, .5);
  font: bold 1.1em/1'trebuchet MS', Arial, Helvetica;
  letter-spacing: 1px;
  text-transform: uppercase;
  border-width: 1px;
  border-style: solid;
  border-color: #fff #ccc #999 #eee;
  background: #c1c1c1;
  background: linear-gradient(#f5f5f5, #c1c1c1);
}
nav a:hover,
nav a:focus {
  outline: 0;
  color: #fff;
  text-shadow: 0 1px 0 rgba(0, 0, 0, .2);
  background: #fac754;
  background: linear-gradient(#fac754, #f8ac00);
}
nav a:active {
  box-shadow: 0 0 2px 2px rgba(0, 0, 0, .3) inset;
}
nav li:first-child a {
  border-left: 0;
  border-radius: 4px 0 0 4px;
}
<nav>
  <ul>
      <li><a href='storingsoverzicht.php'>Storingen</a>
      </li>
      <li><a href='Contacten.php'>Contacten</a>
      </li>
      <li><a href='nieuwestoring.php'>Nieuwe Storing</a>
      </li>
      <li><a href='uitloggen.php'>Uitloggen</a>
      </li>
  </ul>
</nav>

答案 1 :(得分:0)

虽然您的HTML无效,但由于您的内部不应该有div,只需对您的css进行以下更改/添加。

演示:http://jsbin.com/quluhevibe/edit?output

    nav ul {
      width: 900px;
      padding: .7em;
      float: left;
      margin: 0px;
      list-style: none;
      background: #444;
      text-align: center;
      box-sizing: border-box;
    }

   nav ul div {
      display: inline-block;
    }