如何使水平导航菜单居中并且不分割每个链接

时间:2014-04-20 03:25:34

标签: html css alignment stylesheet navbar

我一直在拼命试图将导航菜单设为中心,但我根本无法做到。 我试过显示内联块文本对齐中心和其他东西,但它不会工作当我试图让它帮助将是gr8t

<html>
<style>
* {
    margin: 0px;
    padding: 0px;
    }

body {
    font-family: verdana;
    background-color: #FFF;
    padding: 50px;
    }

h1 {
    text-align: center;
    margin-bottom: 50px;
    }


ul#navmenu, ul.sub1, ul.sub2 {
    list-style-type: none;
    font-size: 9pt;
    }

ul#navmenu li {
    width: 96.6px;
    text-align: center;
    position: relative;
    margin-right: 0px;
    float: left;
    }
       #navmenu ul {
           display: inline-block;
           list-style-type: none;
   }    
ul#navmenu a {
    text-decoration: none;
    display: block;
    width: 96.6px;
    height: 25px;
    line-height: 25px;
    background-color: #2E2E2E;
    border: 1px solid #777777;
    border-top: 2px solid #777777;
    color: white;
    font-family: Arial, sans-serif;
    font-weight: bold;
    font-face: Arial;
    float: left;
    text-align: center;
    }

ul#navmenu .sub1 li {
    }

ul#navmenu .sub1 a {
    margin-top: 0px;
    }

ul#navmenu .sub2 a {
    margin-left: 0px;
    }

ul#navmenu li:hover > a {
    background-color: grey;
    }

ul#navmenu li:hover a:hover {
    background-color: red;
    }

ul#navmenu ul.sub1 {
    display: none;
    position: absolute;
    top: 26px;
    left: 0px;
    }

ul#navmenu ul.sub2 {
    display: none;
    position: absolute;
    top: 0px;
    left: 97.6px;
    }

ul#navmenu li:hover .sub1 {
    display: block;
    }

ul#navmenu .sub1 li:hover .sub2 {
    display: block;
    }

.darrow {
    font-size: 11pt;
    position: absolute;
    top: 5px;
    right: 4px;
    }

#navmenu {
        display: inline-block;
        text-align: center;
        list-style-type: none;
        margin: 0px auto;
        padding: 0px;
        position: relative;
}
.rarrow {
    font-size: 13pt;
    position: absolute;
    top: 6px;
    right: 4px;
    }

#page1 .link1{
color: grey;
border-top: 2px solid red;
}
#page2 .link2{
color: grey;
border-top: 2px solid red;
}
#page3 .link3{
color: grey;
border-top: 2px solid red;
}

</style>
</head>
<body>

<h1> Tom's Navigation Menu</h1>

<div id="page1">
<ul id="navmenu">
<li><a href="dropdown1.html" class="link1">hyperlink 1</a></li>
<li><a href="dropdown2.html" class="link2">hyperlink 2</a><font color="white">
<span class="darrow">&#9660;</font></span>
    <ul class="sub1">
        <li><a href="#">hyperlink 2.1</a></li>
        <li><a href="#">hyperlink 2.2</a></li>
        <li><a href="#">hyperlink 2.3</a></li>
    </ul>
</li>
<li><a href="#">hyperlink 3</a></li>
<li><a href="#">hyperlink 4</a><span class="darrow"><font color="white">&#9660;               

 </font></span> 
    <ul class="sub1">
        <li><a href="#">hyperlink 4.1</a></li>
        <li><a href="#">hyperlink 4.2</a></li>
        <li><a href="#">hyperlink 4.3</a><span class="rarrow">
<font color="white">&#9654;</font></span>
            <ul class="sub2">
                <li><a href="#">hyperlink 4.3.1</a></li>
                <li><a href="#">hyperlink 4.3.2</a></li>
                <li>
<a href="dropdown3.html" class="link3">hyperlink 4.3.3</a></li>
            </ul>
        </li>
    </ul>
</li>
<li><a href="#">hyperlink 5</a></li>
</ul></div>


</body>
</html>

2 个答案:

答案 0 :(得分:0)

尝试将此行添加到CSS中:

#page1 {text-align:center;}

我们正在做的是将#navmenu的父元素#page1设置为text-align:center;

请参阅此jsFiddle:http://jsfiddle.net/JQBSa/

答案 1 :(得分:0)

试试这个: 的 Fiddle Demo CSS

ul#navmenu li {
/* width: 96.6px; */
display: table-cell;
text-align: center;
position: relative;
/* margin-right: 0px; */
/* float: left;*/
}

ul#navmenu, ul.sub1, ul.sub2 {
list-style-type: none;
font-size: 9pt;
width: 100%;
display: table;
}
#navmenu {
display: inline-block;
text-align: center;
list-style-type: none;
margin: 0px auto;
padding: 0px;
position: relative;
}
ul#navmenu a{
text-decoration: none;
display: block;
/* width: 96.6px; */
height: 25px;
line-height: 25px;
background-color: #2E2E2E;
border: 1px solid #777777;
border-top: 2px solid #777777;
color: white;
font-family: Arial, sans-serif;
font-weight: bold;
font-face: Arial;
/* float: left; */
text-align: center;
}