如何使用css将导航栏与中心对齐

时间:2015-02-20 13:45:00

标签: html css css3 nav

我需要帮助将导航栏与中心对齐 这是我的代码它有什么问题?它没有将菜单与中心对齐。

#nav {
  position: absolute;
  left: 0px;
  height: 40px;
  background-color: #2C64B4;
  width: 100%;
  margin: 0 auto;
}
#nav ul {
  margin: 0;
  padding: 0;
  list-style-type: none;
}
#nav ul li {
  margin: 0;
  padding: 0;
  float: left;
}
#nav ul li a {
  text-decoration: none;
  padding: 10px 20px;
  display: block;
  color: #FFF;
  text-align: center;
}
<div id="nav">
  <ul>
    <li><a href="#">Home</a>
    </li>
    <li><a href="#">About Us</a>
    </li>
    <li><a href="#">Blah</a>
    </li>
    <li><a href="#">exampl</a>
    </li>
  </ul>
</div>

8 个答案:

答案 0 :(得分:6)

一种方法是将display的{​​{1}}设置为#nav ul。然后将inline-block添加到父元素,以便将孩子text-align: center

居中

Example Here

ul

或者,您也可以将父元素的#nav { position: absolute; left: 0px; height: 40px; background-color: #2C64B4; width: 100%; text-align: center; } #nav ul { margin: 0; padding: 0; display: inline-block; } display设置为#nav,然后添加flex以使子元素水平居中。

Example Here

justify-content: center

答案 1 :(得分:2)

这里没什么大不了的。

只需将text-align:center添加到ul并制作li display:inline-block

HTML

<div id="nav">
    <ul>
        <li><a href="#">Home</a>
        </li>
        <li><a href="#">About Us</a>
        </li>
        <li><a href="#">Blah</a>
        </li>
        <li><a href="#">exampl</a>
        </li>
    </ul>
</div>

CSS

#nav {
    position: absolute;
    left: 0px;
    height: 40px;
    background-color: #2C64B4;
    width: 100%;
    margin: 0 auto;
}
#nav ul {
    margin: 0;
    padding: 0;
    list-style: none;
    text-align: center;
}
#nav ul li {
    margin: 0;
    padding: 0;
    display: inline-block;
}
#nav ul li a {
    text-decoration: none;
    padding: 10px 20px;
    display: block;
    color: #FFF;
    text-align: center;
}

签入Fiddle

答案 2 :(得分:1)

更新您的ul css 如

#nav ul {
    margin: auto;
     padding: 0;
     overflow: hidden;
     width: 60%;
}

检查小提琴:

http://jsfiddle.net/swapnilmotewar/7kk8knd0/2/

答案 3 :(得分:1)

#nav {
    height: 40px;
    background-color: #2C64B4;
    width: 100%;
    margin: 0 auto;
    }   
#nav ul {
    margin: 0;
    padding: 0;
    text-align:center;
}
#nav ul li {
    margin: 0;
    padding: 0;
    display:inline-block;
    position: relative;
}
#nav ul li a {
    text-decoration: none;
    padding: 10px 20px;
    display: block;
    color: #FFF;
    text-align: center;     
    }

使用此CSS您将获得解决方案。 添加Li相对位置,如果您需要下拉某些类别

答案 4 :(得分:0)

#nav ul {
    margin: auto;
    padding: 0;
}

答案 5 :(得分:0)

您可以将ul设置为:inline-block;并设置text-align:center;到父导航:

#nav {
    position: absolute;
    left: 0px;
    height: 40px;
    background-color: #2C64B4;
    width: 100%;
    text-align:center;
}   
#nav ul {
    margin: 0;
    padding: 0;
    display: inline-block;
}

答案 6 :(得分:0)

#nav {
    position: absolute;
    left: 0px;
    height: 40px;
    background-color: #2C64B4;
    width: 100%;
    margin: 0 auto;
    text-align:center;
}   
#nav ul {
   margin: auto;
     padding: 0;

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <style>
    #nav {
        position: absolute;
        left: 0px;
        height: 40px;
        background-color: #2C64B4;
        width: 100%;
        margin: 0 auto;
    	text-align:center;
    }   
    #nav ul {
       margin: auto;
         padding: 0;
         overflow: hidden;
    	 display: inline-block;
    }
    #nav ul li {
        margin: 0;
        padding: 0;
        float:left;
    }
    #nav ul li a {
        text-decoration: none;
        padding: 10px 20px;
        display: block;
        color: #FFF;
        text-align: center;     
        }
    	</style>
    </head>

    <body>

    <div id="nav">
    <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About Us</a></li>
    <li><a href="#">Blah</a></li>
    <li><a href="#">exampl</a></li>
    </ul>
    </div>
    </body>
    </html>
overflow: hidden; display: inline-block; }

答案 7 :(得分:0)

我是这样做的,它确实有效,但您也应该上课并安排移动设备和桌面设备。我也用于引导程序。

      <nav class="navbar navbar-expand-lg navbar-light sticky-top" style="background-color:#fff;">
          <a class="navbar-brand" href="#">
            <img class="none" src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Brabus_logo.svg/1200px-Brabus_logo.svg.png" width="140" height="70" alt="">
          </a>
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
          <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav">
                <li class="nav-item active">
                    <a class="nav-link none-1" href="index.php">
                        <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Brabus_logo.svg/1200px-Brabus_logo.svg.png" width="140" height="70" alt="">
                    </a>
              </li>     
              <li class="nav-item active">
                <a class="nav-link" href="hakkimizda.php">Hakkımızda</a>
              </li>
              <li class="nav-item active">
                <a class="nav-link" href="arac.php">Araçlarımız</a>
              </li>
              <li class="nav-item active">
                <a class="nav-link" href="iletisim.php">İletişim</a>
              </li>
            </ul>
          </div>
        </nav>
 <style type="text/css">
        @media screen and (max-width: 900px) {
      .none-1 {
        display: none;
      }
    }
    @media screen and (min-width: 900px) {
      .none {
        display: none;
      }
    }
    .navbar-collapse {
        -ms-flex-preferred-size: 100%;
        flex-basis: 100%;
        -ms-flex-positive: 1;
        flex-grow: 1;
        -ms-flex-align: center;
        align-items: center;
        place-content: center;
    }
    </style>