简体   繁体   English

如何使导航栏停留在中间

[英]How to make nav bar stay in the middle

I'm trying to centralize my nav bar, I've tried text align center, and margin auto, but it stays fixed to the left. 我正在尝试将导航栏居中,我尝试了将文本居中对齐,并自动设置了边距,但是它保持固定在左侧。 I also tried to add a width, but still it stays fixed. 我也尝试添加宽度,但是仍然保持不变。 Thanks in advance for your help. 在此先感谢您的帮助。 Please check out the JSFIDDLE . 请签出JSFIDDLE The HTML is as follows: HTML如下:

   <section class="contain">
   <div id="section-nav" class="section-nav">
   <div class="top">
   <ul>
   <li class="logo"><a href="#">Magna Contra</a></li>
   <li class="active"><a href="#">Festival: Paris</a></li>
   <li><a href="#">Festival: Paris</a></li>
   <li><a href="#">Festival: Paris</a></li>
   <li><a href="#">Festival: Paris</a></li>
   </ul>
   </div>
   </section>

And the CSS: 和CSS:

          ul
          {
          list-style-type:none;
          margin:0;
          padding:0;
          }
          li
          {
          display:inline;
          padding:15px;
          text-align: center;
          margin: auto 0;
          }

          li a{
            text-decoration: none;
            color:#bbbbbb;
            font-family: "proxima-nova",sans-serif;
            text-transform: uppercase;
            text-align: center;
            font-size: 0.78em;
          letter-spacing: .2em}

          li a:hover{
            color:white;
          }

          .logo a{
            color:#bbb;
            font-size: 0.78em;
            text-decoration: none;
            text-transform: uppercase;
          }

          .logo a:hover{
          color:white;
          }
          .active a{
            color:white;
          }

          .container {
            display: table;
            width:980px;
            height: 100%;
          }

          .contain{
            display: table;
            width: 100%;
            text-align: center;
            margin: 0 auto;
          }
          .block {
            display: table-row;
            height: 1px;
          }
          .navigation {
            display: inline-block;
            padding: 10px;
            width:100%;
            margin: auto;
            height: 150px;
          }

          .top {
            background-color: #444;
            width:100%;
            display: inline-block;
            padding: 10px;
            text-align: left;

          }

          .navigation:nth-child(odd) {
            background: #222;

          }
          .push {
            height: auto;
          }
          .container {
            margin: 0 auto;
            text-align: center;
          }
          .block:nth-child(odd) {
            background: #fff;
          }


          .search {
          border:0px; 
          background-color:transparent; 
          color:white;
          display: inline-block;
          height:28px;
          }

          .section-nav a{-webkit-transition:400ms;-moz-transition:400ms;-o-transition:400ms;transition:400ms;color:#bbb;font-weight:700;outline:0;text-decoration:none}
          .section-nav a.active,.section-nav a:hover{color:#fff;text-decoration:none}

Your div "section-nav" is not closed. 您的div“ section-nav”未关闭。 I would fix this first. 我先解决这个问题。

You have also applied text-align:left to your .top div, which is the main container for the navigation buttons. 您还对.top div应用了text-align:left,这是导航按钮的主要容器。

The easiest option is to add text-align: center to the ul : 最简单的选择是在ul上添加text-align: center

ul
{
    list-style-type:none;
    margin:0;
    padding:0;
    text-align: center;
}

I would also set the li s to display: inline-block to give you more control over their styling. 我还要将li设置为display: inline-block以使您可以更好地控制其样式。

In the given fiddle you have 在给定的小提琴中

.top {

  text-align: left;

}

change it to 更改为

.top {
   text-align: center
}

fidd -> http://jsfiddle.net/ztyUF/2/ Is this what u ve asked? fidd-> http://jsfiddle.net/ztyUF/2/这是您要问的吗?

I used this when I had the same problem and have pretty much duplicated it for multiple sites. 当我遇到相同的问题并且在多个站点上都重复了很多内容时,就使用了它。 He explains it much better than I can. 他解释得比我好得多。

http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM