1
votes

How do you center the menu with bootstrap?
HTML:

<nav class='navbar navbar-default'>
    <div class='container-fluid container'>
      <div class='navbar-header'>
         <a class='navbar-brand' href='#'>Title</a>
      </div>
      <div id='navbar' class='navbar-collapse'>
          <ul class='nav navbar-nav'>
            <li><a href='#'>Link</a></li>
            <li><a href='#'>Link</a></li>
            <li><a href='#'>Link</a></li>
            <li><a href='#'>Link</a></li>
          </ul>
      </div>
    </div>
  </nav>

It is basic and yet I have tried various solutions and it will not center the menu, it always appears align left. I can center the list elements, but not the list ITSELF.

4

4 Answers

2
votes
@media (min-width:768px) { 
/* centered navigation */
 .nav.navbar-nav {
     float: left;
 }
 .nav.navbar-nav {
     clear: left;
     float: left;
     margin: 0;
     padding: 0;
     position: relative;
     left: 50%;
     text-align: center;
 }
 .nav.navbar-nav > li {
     position: relative;
     right: 50%;
 }
 .nav.navbar-nav li {
    text-align: left
 }
}

http://output.jsbin.com/iJaJAzIM/3/

2
votes

Try this:

.navbar-default > .container-fluid {   
    display: flex;
    justify-content: center;
}

Requires vendor prefixes for IE10. Not supported below IE10.

http://codepen.io/simply-simpy/pen/XjEZNW

0
votes

use the bootstrap class row and use the second .class col-md-6 .col-md-offset-3

this might help you

check the output in jsbin

@charset "utf-8";
/* CSS Document */
body{
    margin:0;
    padding:0;
        }
.navbar.navbar-default{
    background-image:none;
    background-color:rgba(89,63,127,0.79);
    border:none;
}
.navbarheader{
    color:#FFFFFF;
    margin-top:-10px;
    font-family: 'Lobster', cursive;
}
.nav,.navbar-nav{
    font-size:15px;
    color:white;

}
<html>
  <head>
    <title>Landing Page</title>
    <link rel="stylesheet" type="text/css" href="MylandingSS.css"/>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity=      "sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet"/>
  </head>
  <body>
    <div class="navbar navbar-default navbar-fixed-top">
      <!----------------HEADER------------------------------>
        <div class="navbar-header navbar-brand header"><h1 class="navbarheader">abcdefg</h1></div>
     <!------------------LINKS----------------------------------->
    <div class="row">
      <div class="col-md-6 col-md-offset-3">
        <ul class="nav navbar-nav">
        <li><a href="">Page 1</a></li>
        <li><a href="">Page 2</a></li>
        <li><a href="">Page 3</a></li>
        </ul>
      </div>  
  </div>

  </div>

  </body>
</html>
0
votes

To center the content of the entire navbar, (centering both the brand and links)..

.navbar .navbar-header,
.navbar-collapse {
    float:none;
    display:inline-block;
    vertical-align: top;
}

@media (max-width: 768px) {
    .navbar-collapse  {
        display: block;
    }
}

http://codeply.com/go/1lrdvNH9GI

If you want to center only the links, see this question: Center content in responsive bootstrap navbar