Bootstrap 3:如何制作居中的导航栏

时间:2013-08-30 14:32:43

标签: css twitter-bootstrap twitter-bootstrap-3

我已经看过Bootstrap< = 2的一些黑客但是我正在使用v3而我想创建一个水平行的链接,这些链接位于行/容器的中心。这是我的标记:

<div class="footer row">    
    <div class="col-12">                
        <ul id="menu-main" class="nav navbar-nav">
            <li class="text-center">
                <%= link_to "Home", root_path  %>
            </li>
            <li class="text-center">
                <%= link_to "About", root_path  %>          
            </li>
            <li class="text-center">
                <%= link_to "Help", root_path  %>           
            </li>
        </ul>
    </div>  
</div>

5 个答案:

答案 0 :(得分:69)

这应该是您正在寻找的。

Here is a jsFiddle Demo

如果您希望将其作为固定页脚,只需将navbar-fixed-bottom类添加到<nav class="navbar navbar-default" role="navigation">元素。

<强> HTML

<nav class="navbar navbar-default" role="navigation">
   <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
         <span class="sr-only">Toggle navigation</span>
         <span class="icon-bar"></span>
         <span class="icon-bar"></span>
         <span class="icon-bar"></span>
      </button>
   </div>

   <div class="collapse navbar-collapse  navbar-ex1-collapse">
      <ul class="nav navbar-nav">
         <li class="active"><a href="#">Link</a></li>
         <li><a href="#">Link</a></li>
         <li><a href="#">Link</a></li>
      </ul>
  </div>
</nav>

<强> CSS

@media (min-width: 768px){
    .navbar-nav{
        float:none;
        margin: 0 auto;
        display: table;
        table-layout: fixed;
    }
}

答案 1 :(得分:1)

与@ Adam的建议相似,我发现这基本上有效:

    <div class="footer row">    

            <ul id="menu-main" class="list-inline text-center">
                <li class="text-center">
                    <%= link_to "Home", root_path  %>
                </li>
                <li class="text-center">
                    <%= link_to "About", root_path  %>          
                </li>
                <li class="text-center">
                    <%= link_to "Help", root_path  %>           
                </li>
                <li class="text-center">
                    <%= mail_to "e@streetofwalls.com", "Contact"   %>
                </li>
            </ul>
    </div>

但是,它不会像导航栏那样在较小的宽度上折叠。如果有人知道如何保持这种功能将是很好的。

答案 2 :(得分:1)

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />

    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <ul class="nav navbar-nav">
          <li><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Help</a></li>
        </ul>
      </div>
    </nav>
lab<-c("NO[2]","NO","CO[2]","CO")
lab2<-noquote(lab)
plot(1,1,xlab=bquote(.(lab[1])))
plot(1,1,xlab=bquote(.(lab2[1])))

答案 3 :(得分:1)

&#13;
&#13;
#menu-main{
  width: 100%;
  text-align: center;
  display:block;
}
#menu-main li {
    display: inline-block;
    float: none;
    vertical-align: top;
}
#menu-main a{
  display: block;
  padding: 10px;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="footer row">    
    <div class="col-12">                
        <ul id="menu-main" class="nav navbar-nav">
            <li>
                <a href="#">Home</a>
            </li>
            <li>
                <a href="#">About</a>
            </li>
            <li>
                <a href="#">Help</a>
            </li>
        </ul>
    </div>  
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

我建议给导航栏留出25%的边距:

CSS

@media (min-width: 768px) {
  .centered-ul{
    margin-left:25%;
  }
}

HTML

<ul class="nav navbar-nav centered-ul">
...
</ul>