2017-03-09 82 views
0

我該如何居中這個下拉菜單?菜單出現在左側,而不是中央。這讓我很難過。文本對齊:中心沒有爲我工作。提前謝謝你。如何居中這些菜單元素?

<style> 
    #dropnav 
    {text-align: center; width: 960px; margin: 0 auto; display: inline-block; } 

    #dropnav ul 
    {text-align: center; 
     list-style:none; 
     position:relative; 
     float:left; 
     margin:0; 
     padding:0 
    } 

    #dropnav ul a 
    { 
     display:block; 
     color:#333; 
     text-decoration:none; 
     font-weight:700; 
     font-size:12px; 
     line-height:32px; 
     padding:0 15px; 
     font-family:"HelveticaNeue","Helvetica Neue",Helvetica,Arial,sans-serif 
    } 

    #dropnav ul li 
    { 
     position:relative; 
     float:left; 
     margin:0; 
     padding:0 
    } 

    #dropnav ul li.current-menu-item 
    { 
     background:#ddd 

    } 

    #dropnav ul li:hover 
    { 
     background:#f6f6f6 
    } 

    #dropnav ul ul 
    { 
     display:none; 
     position:absolute; 
     top:100%; 
     left:0; 
     background:#fff; 
     padding:0 
    } 

    #dropnav ul ul li 
    { 
     float:none; 
     width:200px 
    } 

    #dropnav ul ul a 
    { 
     line-height:120%; 
     padding:10px 15px 
    } 

    #dropnav ul ul ul 
    { 
     top:0; 
     left:100% 
    } 

    #dropnav ul li:hover > ul 
    { 
     display:block 
    } 
</style> 

HTML

<nav id="dropnav"> 
<ul> 
    <li class="current-menu-item"><a href="#">Option 1</a></li> 

    <li><a href="#">Option 2</a> 
    <ul> 
     <li><a href="#">Sub Menu 1</a></li> 
     <li><a href="#">Sub Menu 2</a></li> 
     <li><a href="#">Sub Menu 3</a></li> 
     <li><a href="#">Sub Menu 4</a></li> 
    </ul> 
    </li> 
    </ul></nav> 

回答

0

你不想父容器是inline-block,你希望它是block代替。而且您不想將頂層菜單中的li浮動 - 請改爲使用inline-block,然後刪除標記中這些元素之間的空白,以便屏幕上的元素之間沒有空隙。

#dropnav { 
 
    text-align: center; 
 
    width: 960px; 
 
    margin: 0 auto; 
 
} 
 

 
#dropnav ul { 
 
    text-align: center; 
 
    list-style: none; 
 
    position: relative; 
 
    margin: 0; 
 
    padding: 0 
 
} 
 

 
#dropnav ul a { 
 
    display: block; 
 
    color: #333; 
 
    text-decoration: none; 
 
    font-weight: 700; 
 
    font-size: 12px; 
 
    line-height: 32px; 
 
    padding: 0 15px; 
 
    font-family: "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif 
 
} 
 

 
#dropnav ul li { 
 
    position: relative; 
 
    display: inline-block; 
 
    margin: 0; 
 
    padding: 0 
 
} 
 

 
#dropnav ul li.current-menu-item { 
 
    background: #ddd 
 
} 
 

 
#dropnav ul li:hover { 
 
    background: #f6f6f6 
 
} 
 

 
#dropnav ul ul { 
 
    display: none; 
 
    position: absolute; 
 
    top: 100%; 
 
    left: 0; 
 
    background: #fff; 
 
    padding: 0 
 
} 
 

 
#dropnav ul ul li { 
 
    float: none; 
 
    width: 200px 
 
} 
 

 
#dropnav ul ul a { 
 
    line-height: 120%; 
 
    padding: 10px 15px 
 
} 
 

 
#dropnav ul ul ul { 
 
    top: 0; 
 
    left: 100% 
 
} 
 

 
#dropnav ul li:hover > ul { 
 
    display: block 
 
}
<nav id="dropnav"> 
 
    <ul> 
 
    <li class="current-menu-item"><a href="#">Option 1</a></li><li><a href="#">Option 2</a> 
 
     <ul> 
 
     <li><a href="#">Sub Menu 1</a></li> 
 
     <li><a href="#">Sub Menu 2</a></li> 
 
     <li><a href="#">Sub Menu 3</a></li> 
 
     <li><a href="#">Sub Menu 4</a></li> 
 
     </ul> 
 
    </li> 
 
    </ul> 
 
</nav>

+0

你解決它邁克爾。萬分感謝。 – Rose

+0

@玫瑰真棒,不客氣:) –