2016-09-24 36 views
1

我环顾四周,无法找到如何水平居中我的固定导航栏。如何水平居中固定导航栏?

如果你能,你可以告诉我如何解释原因。

谢谢。

#nav { 
 
    list-style: none; 
 
    position: fixed; 
 
    margin-top: -10px; 
 
    width: 100%; 
 
    font-family: Product Sans; 
 
    font-size: 0; 
 
} 
 
#nav li { 
 
    width: 200px; 
 
    height: 50px; 
 
    display: inline-block; 
 
    text-align: center; 
 
    padding-top: 5px; 
 
    background-color: white; 
 
    font-size: 30px; 
 
}
<ul id="nav"> 
 
    <li>stack</li> 
 
    <li>over</li> 
 
    <li>flow</li> 
 
</ul>

回答

1

你必须居中对齐UL

内里的物品,因此CSS将

<style> 

#nav{ 
    list-style: none; 
    position: fixed; 
    margin-top: -10px; 
    width: 100%; 
    font-family: Product Sans; 
    font-size: 0; 
    text-align:center; 
    padding-left:0px; 
} 
#nav li{ 
    width: 200px; 
    height: 50px; 
    display: inline-block; 
    text-align: center; 
    padding-top: 5px; 
    background-color: white; 
    font-size: 30px; 
} 


</style> 

而且让HTML没有任何变化

0

您只需要添加text-align: center;#nav,像这样:

<style> 
#nav{ 
    list-style: none; 
    position: fixed; 
    margin-top: -10px; 
    width: 100%; 
    font-family: Product Sans; 
    font-size: 0; 
    text-align: center; /* <--- */ 
} 
</style>