Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 3: How to make a centered navbar

Tags:

I've seen some hacks for Bootstrap <=2 but I'm using v3 and I want to make a horizontal row of links that are centered within the row/container. Here's my markup:

<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>
like image 568
emersonthis Avatar asked Aug 30 '13 14:08

emersonthis


2 Answers

This should be exactly what you are looking for.

Here is a jsFiddle Demo

If you want this as a fixed-footer, just add navbar-fixed-bottom class to the <nav class="navbar navbar-default" role="navigation"> element.

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;
    }
}
like image 150
Schmalzy Avatar answered Sep 20 '22 15:09

Schmalzy


.nav.navbar-nav {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
}

@media (min-width: 768px) {
  .navbar-nav {
    float: none;
  }
}
    <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>
like image 39
antelove Avatar answered Sep 21 '22 15:09

antelove