导航菜单不会居中

时间:2015-02-11 16:42:37

标签: css menu alignment center

我的导航菜单不会居中。我无法在代码中找到错误。也许有人可以帮助我?!

#nav-header {
    height: 60px;
    width: auto;
    background-color: rgba(255, 255, 255, 0.7);
    margin-top: 80px;
}

#nav-header li.nav-link {
    display: inline;
    padding: 20px 20px 20px 20px;
    font-family: 'Source Sans Pro', sans-serif,"Open Sans", "Helvetica",sans-serif;
    font-size: 28pt;
    text-align: inherit;
    margin: 0 auto;

    
}
 <ul id="nav-header">
    <li class="nav-link"><a href="index.html">Home</a></li>
    <li class="nav-link"><a href="#">Game</a></li>
    <li class="nav-link"><a href="#">Higschore</a></li>
    <li class="nav-link"><a href="#">Contact</a></li>
</ul> 

亲切的问候,

Jelmer

3 个答案:

答案 0 :(得分:3)

text-align:center;添加到父元素。

&#13;
&#13;
#nav-header {
    height: 60px;
    width: auto;
    background-color: rgba(255, 255, 255, 0.7);
    margin-top: 80px;
    text-align:center; /* see here */
}

#nav-header li.nav-link {
    display: inline-block; /* preferable to plain inline */
    padding: 20px 20px 20px 20px;
    font-family: 'Source Sans Pro', sans-serif,"Open Sans", "Helvetica",sans-serif;
    font-size: 28pt;
    text-align: inherit;
    margin: 0 auto;

    
}
&#13;
 <ul id="nav-header">
    <li class="nav-link"><a href="index.html">Home</a></li>
    <li class="nav-link"><a href="#">Game</a></li>
    <li class="nav-link"><a href="#">Higschore</a></li>
    <li class="nav-link"><a href="#">Contact</a></li>
</ul> 
&#13;
&#13;
&#13;

答案 1 :(得分:2)

text-align: center添加到#nav-header

#nav-header {
    height: 60px;
    width: auto;
    background-color: rgba(255, 255, 255, 0.7);
    margin-top: 80px;
    text-align: center;
}

#nav-header li.nav-link {
    display: inline;
    padding: 20px 20px 20px 20px;
    font-family: 'Source Sans Pro', sans-serif,"Open Sans", "Helvetica",sans-serif;
    font-size: 28pt;
    text-align: inherit;
    margin: 0 auto;

    
}
<ul id="nav-header">
    <li class="nav-link"><a href="index.html">Home</a></li>
    <li class="nav-link"><a href="#">Game</a></li>
    <li class="nav-link"><a href="#">Higschore</a></li>
    <li class="nav-link"><a href="#">Contact</a></li>
</ul>

答案 2 :(得分:1)

首先,您需要重置padding的默认ul,然后因为您使用inline li元素,您可以使用{{1}容器中的属性将元素置于内部:

text-align

#nav-header {
   padding:0;
   text-align:center;
}
#nav-header {
  height: 60px;
  width: auto;
  background-color: rgba(255, 255, 255, 0.7);
  margin-top: 80px;
  padding:0;
  text-align:center;
}
#nav-header li.nav-link {
  display: inline;
  padding: 20px 20px 20px 20px;
  font-family: 'Source Sans Pro', sans-serif, "Open Sans", "Helvetica", sans-serif;
  font-size: 28pt;
  text-align: inherit;
  margin: 0 auto;
}