导航菜单不会居中

Nav menu will not 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> 

亲切的问候,

杰尔默

首先,您需要重置 ul 的默认值 padding,然后由于您对 li 元素使用 inline,您可以使用 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;
}
<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> 

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>

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

#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;

    
}
 <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>