我如何将导航栏居中?

时间:2013-06-12 10:28:48

标签: html css

我的导航栏中有一个脚本:

<style type="text/css">
 /* Navigation Bar */
 #nav_bar {
     display:inline-block;
     height:50px;
 }
 #nav_bar ul {
     display:inline-block;
     list-style-type: none;
     border: 1px solid red;
     width: 565px;
     height: 100%;
     text-align: center;
     padding: 0;
     margin: 0;
 }
 #nav_bar li {
     display:inline;
     height:100%;
     padding: 0;
     margin: 0;
 }
 #nav_bar a:hover {
     background-color: #000000;
 }
 #nav_bar a {
     display:inline-block;
     height: 100%;
     color:white;
     text-decoration:none;
     line-height: 50px;
     padding: 0 1em 0 1em;
     background-color: #900000;
 }
</style>
</font>

我在尝试将其显示在页面中央时遇到问题,我该怎么办? 我已查看&#34;显示:inline-block;&#34;和&#34;职位:亲属&#34;并且无法找到有效的代码 导航栏的html部分如下(关于你的评论)我希望它有所帮助! :)

<div id="nav_bar">
            <ul>
                <li> <a href="#">Home</a> </li>
                <li> <a href="#">Forums</a> </li>
                <li> <a href="#">Shipping Info</a> </li>
                <li> <a href="#">Contact us</a> </li>
                <li> <a href="#">About us</a> </li>
            </ul>
        </div>

4 个答案:

答案 0 :(得分:1)

给它一个宽度和自动边距,并确保它是一个块级元素。

默认情况下,'div'是块级元素,因此您可以删除此规则。

您必须设置宽度或菜单,并展开其容器的宽度。

#nav_bar {
     display:block;
     height:50px;
     margin: 0 auto;
     width: 567px; /* or whatever you require */
 }

实施例: http://jsfiddle.net/29FRa/

答案 1 :(得分:0)

 #nav_bar {
   height:50px;
   width:800px;
   margin:0 auto;
 }

HTML:

<html>
 <body>
  <div id="#nav_bar"></div>
 </body>
</html>

答案 2 :(得分:0)

text-align: center;上使用#nav_bar,并确保它是块级元素。

http://jsfiddle.net/TKMeU/

答案 3 :(得分:0)

共有六种方法: 1,边距和宽度达到水平中心

#nav_bar {
     height:50px;
 }
 #nav_bar ul {
     list-style-type: none;
     border: 1px solid red;
     width: 565px;
     height: 100%;
     text-align: center;
     padding: 0;
     margin-left: auto;
    margin-right: auto;
 }

请查看demo。 2,使用内联块,如下所示:

#nav_bar {
     height:50px;
     text-align: center;
 }
 #nav_bar ul {
     list-style-type: none;
     display: inline-block;
     border: 1px solid red;
     width: 565px;
     height: 100%;
     text-align: center;
     padding: 0;
    text-align: center;
    font-size: 0;
    letter-spacing: -4px;
    word-spacing: -4px;
 }
 #nav_bar li {
    margin: 0 5px;
   display: inline-block;
  *display: inline;
  zoom:1;
  letter-spacing: normal;  
  word-spacing: normal;
  font-size: 12px;
 }

请查看demo

3,使用float,就像这样:

#nav_bar {
     float: left;
     width: 100%;
     overflow: hidden;
     position: relative;
 }
 #nav_bar ul {
     list-style-type: none;
     width: 565px;
      height: 50px;
     height: 100%;
     padding: 0;
     clear: left;
      float: left;
      position: relative;
      left: 50%;/*整个分页向右边移动宽度的50%*/
      text-align: center;
 }
 #nav_bar li {
     margin: 0 5px;
     display: block;
     height: 50px;
     float: left;
     position: relative;
     right: 50%;/*将每个分页项向左边移动宽度的50%*/
 }
 #nav_bar a:hover {
     background-color: #000000;
 }
 #nav_bar a {
     display:block;
     height: 100%;
     color:white;
     text-decoration:none;
     line-height: 50px;
     padding: 0 1em 0 1em;
     background-color: #900000;
 }

请查看demo

其他方法,您可以点击here