0

I am trying to center the content of my navbar but for some reason text-align:center does not work. Here is my HTML:

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
  <!-- NAVBAR -->
  <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
      <div class="navbar-header page-scroll">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar_clps">
          <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 main-nav navbar-collapse" id="navbar_clps">
        <ul class="nav navbar-nav">
          <!--<li class="hidden"><a class="page-scroll" href="#page-top"></a></li>-->
          <li><a class="page-scroll color_animation" href="#about">About</a></li>
          <li><a class="page-scroll color_animation" href="#services">Services</a></li>
          <li><a class="page-scroll color_animation" href="#reservation">Reservation</a></li>
          <li><a class="page-scroll color_animation" href="#contact">Contact us</a></li>
          <li><a id="login_lnk" class="page-scroll color_animation" href="#login" data-toggle="modal">Login</a></li>
        </ul>
      </div>
      <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
  </nav>
</body>

And here is the CSS:

.main-nav a {
  font-size: 20px;
  font-weight: 700;
  text-decoration: none;
  color: #000;
  display: block;
  text-align: center;
  padding: 2px 0;
  transition: color 0.3s ease-in-out;
}

.main-nav {
  text-align: center !important;
  /*margin:  auto 0 auto;*/
}

li {
  color: white !important;
  word-spacing: 5px !important;
}

I know this question has been asked before, i even looked at some of the questions but they didn't help me(as i said, text-align doesn't work, its not a duplicate since the solution there was text-align).

vanburen
  • 21,502
  • 7
  • 28
  • 41
evans
  • 58
  • 8

3 Answers3

1

.navbar is floated left, just set it to float:none; display: inline-block; and and finally make the container text-align: center;

.main-nav a {
        font-size: 20px;
        font-weight: 700;
        text-decoration: none;
        color: #000;
        display: block;
        text-align: center;
        padding: 2px 0;
        transition: color 0.3s ease-in-out;
}

 .nav-center{
   display: inline-block;
   float: none !important;
 } 

    li{
    color: white !important;
    word-spacing: 5px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">
            <div class="navbar-header page-scroll">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar_clps">
                    <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 main-nav navbar-collapse text-center navbar_clps">
                <ul class="nav navbar-nav nav-center">
<!--                    <li class="hidden"><a class="page-scroll" href="#page-top"></a></li>-->
                    <li><a class="page-scroll color_animation" href="#about">About</a></li>
                    <li><a class="page-scroll color_animation" href="#services">Services</a></li>
                    <li><a class="page-scroll color_animation" href="#reservation">Reservation</a></li>
                    <li><a class="page-scroll color_animation" href="#contact">Contact us</a></li>
                    <li><a id="login_lnk"class="page-scroll color_animation" href="#login" data-toggle="modal" >Login</a></li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container -->
    </nav>
Ron.Basco
  • 2,348
  • 16
  • 25
  • So that means that the default state is float:left ? – evans Jan 02 '17 at 07:11
  • thats right. one last thing you have an error targetting the nav-bar on button click when it was collapsed. you need to use `period(.) on class names and hash(#) on id's. ` data-target=".navbar_clps"`, this is a class selector and you set the navbar_clps` as an id. I already changed it and put it as class name instead of id – Ron.Basco Jan 02 '17 at 07:16
  • Oh right, thanks for the tip man! – evans Jan 02 '17 at 07:19
0

You can use CSS Flexbox. Like:

/* Use media query to only show it on tablets & desktops (to prevent menu from breaking on mobiles) */
@media screen and (min-width: 768px) {
  #navbar_clps {
    display: flex !important;
    justify-content: center;
  }
}

Have a look at the snippet below (use full screen):

.main-nav a {
  font-size: 20px;
  font-weight: 700;
  text-decoration: none;
  color: #000;
  display: block;
  text-align: center;
  padding: 2px 0;
  transition: color 0.3s ease-in-out;
}

.main-nav {
  text-align: center !important;
  /*margin:  auto 0 auto;*/
}

li {
  color: white !important;
  word-spacing: 5px !important;
}

@media screen and (min-width: 768px) {
  #navbar_clps {
    display: flex !important;
    justify-content: center;
  }
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
    <!-- NAVBAR -->
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">
            <div class="navbar-header page-scroll">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar_clps">
                    <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 main-nav navbar-collapse" id="navbar_clps">
                <ul class="nav navbar-nav">
<!--                    <li class="hidden"><a class="page-scroll" href="#page-top"></a></li>-->
                    <li><a class="page-scroll color_animation" href="#about">About</a></li>
                    <li><a class="page-scroll color_animation" href="#services">Services</a></li>
                    <li><a class="page-scroll color_animation" href="#reservation">Reservation</a></li>
                    <li><a class="page-scroll color_animation" href="#contact">Contact us</a></li>
                    <li><a id="login_lnk"class="page-scroll color_animation" href="#login" data-toggle="modal" >Login</a></li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container -->
    </nav>
</body>

Hope this helps!

Saurav Rastogi
  • 9,575
  • 3
  • 29
  • 41
-1

Add this CSS

.navbar-nav {
  float: none !important;
  margin: 0px auto !important;
  width: 437px !important;
}
aavrug
  • 1,849
  • 1
  • 12
  • 20