【问题标题】:Trouble centering fixed navigation bar固定导航栏无法居中
【发布时间】:2016-01-13 07:30:20
【问题描述】:

基本上,我希望在我的网站上有一个固定的导航栏,当你向下滚动时它会随着你移动。到目前为止,一切都很好,但我真的很难将酒吧居中。

HTML

<header id="header">
<nav id="nav">
    <ul>
    <li><a href="#slide1">Front</a></li>
    <li><a href="#slide2">Mastering</a></li>
    <li><a href="#slide3">Samples</a></li>
    <li><a href="#slide4">Contact</a></li>
    </ul>
</nav>
</header>

CSS

#header {
z-index: 1;
width: 100%;
height: 80px;
position: fixed;
background-color:rgba(0,0,0,0.8);
display: block;
}

#nav { 
z-index: 1;
width:100%;
margin-top: 20px;
display: block;
position: fixed;
text-align:center;
}

#nav ul{
list-style: none;
display: block;
margin: 0 auto;
width: 50%;
/* To help to identify the location */
height:50px;
background-color:green;
}

#nav li{
margin-top: 9px;
float: left;
padding-left: 21px;
display:block;
}

JSFiddle https://jsfiddle.net/kjr1591d/1/

很想得到一些帮助。提前谢谢你:)

【问题讨论】:

标签: html css navigation nav


【解决方案1】:

我认为您正在寻找类似的东西-

#header {
    z-index: 1;
    width: 100%;
    height: 80px;
    position: fixed;
    background-color:rgba(0,0,0,0.8);
    display: block;
}

#nav { 
    z-index: 1;
    width:100%;
    margin: 15px 0;
    display: block;
    position: fixed;
    text-align:center;
}

#nav ul{
    list-style: none;
    display: block;
    margin: 0 auto;
    width: 50%;
    /* To help to identify the location */
    height:50px;
    background-color:green;
    text-align:center;
    padding:0
}

#nav li{
    margin:15px 0 0;
    /*float: left;*/
    padding:0 10px;
    display:inline-block;
}
<header id="header">           
    <nav id="nav">
        <ul>
            <li><a href="#slide1">Front</a></li>
            <li><a href="#slide2">Mastering</a></li>
            <li><a href="#slide3">Samples</a></li>
            <li><a href="#slide4">Contact</a></li>
        </ul>
    </nav>
</header>

【讨论】:

    【解决方案2】:

    jiddle

    #nav ul{
    list-style: none;
    display: block;
    margin: 0 auto;
    width: 80%;
    /* To help to identify the location */
    height:50px;
    background-color:green;
    }
    

    【讨论】:

      【解决方案3】:

      ulli 更改为显示为inline-block,并为li 分配相同大小的左右边距。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-26
        • 1970-01-01
        • 2013-11-27
        • 2019-10-13
        • 2015-06-09
        • 2023-04-01
        • 2014-06-18
        相关资源
        最近更新 更多