Bootstrap 3: How to make a centered navbar

Bootstrap 3: How to make a centered navbar

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:

Solutions/Answers:

Solution 1:

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;
    }
}

Solution 2:

Similar to @Adam’s suggestion, I see that this basically works:

    <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>

However, it won’t collapse on smaller widths like a navbar. It would be nice to maintain this functionality if someone knows how.

Solution 3:

.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>

Solution 4:

#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;
}
<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>

References

Related:  Accessing angular bootstrap modal form from controller